Java - plus() and minus() Period to add and subtract one period from another. and normalize()

Introduction

Use the normalized() method of the Period class to normalize the years and months.

The method ensures that the month value stays within 0 to 11.

For example, a period of "2 years and 15 months" will be normalized to "3 years and 3 months."

Demo

import java.time.Period;

public class Main {
  public static void main(String[] args) {
    Period p1 = Period.of(2, 3, 5);
    Period p2 = Period.of(1, 15, 28);

    System.out.println("p1: " + p1);
    System.out.println("p2: " + p2);
    System.out.println("p1.plus(p2): " + p1.plus(p2));
    System.out.println("p1.plus(p2).normalized(): " + p1.plus(p2).normalized());
    System.out.println("p1.minus(p2): " + p1.minus(p2));

  }/* w w w  .ja  va 2s . c o m*/
}

Result

Related Topic