Period Arithmetic and Normalization
You'll learn how to add or subtract Period objects and understand how Java normalizes (or doesn't normalize) years, months, and days.
Performing Arithmetic with Period
Just like with Duration, you can add or subtract Period objects using plus() and minus(). This lets you combine multiple date-based amounts or adjust them dynamically:
Period period1 = Period.ofMonths(6);
Period period2 = Period.ofDays(15);
Period combined = period1.plus(period2);
System.out.println(combined); // P6M15D (6 months, 15 days)
Period reduced = combined.minus(Period.ofMonths(2));
System.out.println(reduced); // P4M15D (4 months, 15 days)
Understanding Normalization Behavior
Here's the crucial part: Period does NOT automatically normalize months to years or days to months. Why? Because date units aren't uniform—months have different lengths (28–31 days), so Java can't safely convert between them without a specific date context.
Period thirteenMonths = Period.ofMonths(13);
System.out.println(thirteenMonths); // P13M (not P1Y1M!)
Period fortyDays = Period.ofDays(40);
System.out.println(fortyDays); // P40D (not P1M10D or similar)
Manual normalization happens only for the normalized() method, which converts excess months to years:
Period period = Period.of(0, 13, 0);
Period normalized = period.normalized();
System.out.println(normalized); // P1Y1M (13 months → 1 year + 1 month)
Note: normalized() does NOT convert days—only months to years, because 12 months always equals 1 year, but days-per-month varies.
Key Takeaway:
Period arithmetic with plus() and minus() combines components directly without automatic normalization. Use normalized() to convert excess months to years, but days remain unconverted because their relationship to months depends on calendar context.