Java - Perform additions, subtractions, multiplications, and negation on a period.

Introduction

The division operation performs an integer division, for example, 7 divided by 3 is 2.

The following snippet of code shows some of the operations and their results on periods:

Demo

import java.time.Period;

public class Main {
  public static void main(String[] args) {
    Period p1 = Period.ofDays(15);  // P15D
    Period p2 = p1.plusDays(12);    // P27D
    Period p3 = p1.minusDays(12);   // P3D
    Period p4 = p1.negated();       // P-15D
    Period p5 = p1.multipliedBy(3); // P45D
    System.out.println(p1);/*from   ww w .j a v a 2 s  . c  o m*/
    System.out.println(p2);
    System.out.println(p3);
    System.out.println(p4);
    System.out.println(p5);
  }
}

Result

Related Topic