java.time.Period.normalized() Method Example



Description

The java.time.Period.normalized() method returns a copy of this period with the years and months normalized.

Declaration

Following is the declaration for java.time.Period.normalized() method.

public Period normalized()

Return Value

a Period based on this period with excess months normalized to years, not null.

Exception

ArithmeticException − if numeric overflow occurs.

Example

The following example shows the usage of java.time.Period.normalized() method.

package com.tutorialspoint;

import java.time.Period;

public class PeriodDemo {
   public static void main(String[] args) {

      Period period = Period.of(1,15,2);
      System.out.println("Years: " + period.getYears() 
         + ", Months: " + period.getMonths()
         +", Days: " + period.getDays());
      Period period1 = period.normalized();
      System.out.println("Years: " + period1.getYears() 
         + ", Months: " + period1.getMonths()
         +", Days: " + period1.getDays());
   }
}

Let us compile and run the above program, this will produce the following result −

Years: 1, Months: 15, Days: 2
Years: 2, Months: 3, Days: 2
Advertisements