Thursday, August 28, 2025
HomeLanguagesJavaChronoPeriod negated() method in Java with Examples

ChronoPeriod negated() method in Java with Examples

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

Syntax:

ChronoPeriod normalized()

Parameters: This function does not accepts any parameter.

Return Value: This function returns a new instance of ChronoPeriod 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:




// Java code to show the function to normalize
// months and years of the period
  
import java.time.*;
import java.time.chrono.*;
import java.time.temporal.ChronoUnit;
  
public class ChronoPeriodClass {
  
    // Function to normalize given periods
    static void toNormalize(ChronoPeriod 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;
        ChronoPeriod p1 = Period.of(year, months, days);
  
        toNormalize(p1);
    }
}


Output:

P5Y3M10D

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




// Java code to show the function to normalize
// months and years of the period
  
import java.time.*;
import java.time.chrono.*;
import java.time.temporal.ChronoUnit;
  
public class ChronoPeriodClass {
  
    // Function to normalize given periods
    static void toNormalize(ChronoPeriod 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;
        ChronoPeriod p1 = Period.of(year, months, days);
  
        toNormalize(p1);
    }
}


Output:

P12Y1M366D

Reference: https://docs.oracle.com/javase/9/docs/api/java/time/chrono/ChronoPeriod.html#normalized–

RELATED ARTICLES

Most Popular

Dominic
32244 POSTS0 COMMENTS
Milvus
80 POSTS0 COMMENTS
Nango Kala
6615 POSTS0 COMMENTS
Nicole Veronica
11787 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11832 POSTS0 COMMENTS
Shaida Kate Naidoo
6728 POSTS0 COMMENTS
Ted Musemwa
7009 POSTS0 COMMENTS
Thapelo Manthata
6684 POSTS0 COMMENTS
Umr Jansen
6697 POSTS0 COMMENTS