Close

Java Date Time - Period.normalized() Examples

Java Date Time Java Java API 


Class:

java.time.Period

java.lang.Objectjava.lang.Objectjava.time.Periodjava.time.Periodjava.time.chrono.ChronoPeriodChronoPeriodjava.io.SerializableSerializableLogicBig

Method:

public Period normalized()

This method returns a copy of this period with the years and months normalized. The months unit is adjusted to have an absolute value less than 11, with the years unit being adjusted to compensate. For example, a period of "1 Year and 15 months" will be normalized to "2 years and 3 months". This method leaves the days unit unchanged

The sign of the years and months units will be the same after normalization.


Examples


package com.logicbig.example.period;

import java.time.Period;

public class NormalizedExample {

public static void main(String... args) {
Period p = Period.of(100, 100, 20);
System.out.println(p);

Period p2 = p.normalized();
System.out.println(p2);
}
}

Output

P100Y100M20D
P108Y4M20D




This example shows that normalized() method does not change the days.

package com.logicbig.example.period;

import java.time.Period;

public class NormalizedExample2 {

public static void main(String... args) {
Period p = Period.of(100, -10, 200);
System.out.println(p);

Period p2 = p.normalized();
System.out.println(p2);
}
}

Output

P100Y-10M200D
P99Y2M200D




See Also