Period normalized() method in Java with Examples

The normalized() method of Period class in Java is used to return a new instance of Period after normalizing years and months.

Syntax:

public Period normalized()

Parameters: This function does not accepts any parameter.



Return Value: This function returns a new instance of Period after normalizing year and month of the period.

Exceptions: It throws an ArithmeticException. This exception is caught if numeric overflow occurs.

Below program illustrates the above method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to show the function to normalize
// months and years of the period
import java.time.Period;
import java.time.temporal.ChronoUnit;
  
public class PeriodClass {
  
    // Function to normalize given periods
    static void toNormalize(Period p1)
    {
  
        System.out.println(p1.normalized());
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        // Defining period
        int year = 4;
        int months = 15;
        int days = 10;
        Period p1 = Period.of(year, months, days);
  
        toNormalize(p1);
    }
}

chevron_right


Output:

P5Y3M10D

Program 2: This will not normalize the number of days.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to show the function to normalize
// months and years of the period
import java.time.Period;
import java.time.temporal.ChronoUnit;
  
public class PeriodClass {
  
    // Function to normalize given periods
    static void toNormalize(Period p1)
    {
  
        System.out.println(p1.normalized());
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        // Defining period
        int year = 10;
        int months = 25;
        int days = 366;
        Period p1 = Period.of(year, months, days);
  
        toNormalize(p1);
    }
}

chevron_right


Output:

P12Y1M366D

Reference: https://docs.oracle.com/javase/8/docs/api/java/time/Period.html#normalized–



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.