Sub topic 12
Measuring Date-Based Amounts with Period
A Period is an amount of time measured in calendar units: years, months, and days. It answers questions like "How old is this person?" or "When does a 6-month contract end?" Use Duration, covered in the previous chapter, for hours, minutes, and seconds. Use Period for calendar dates.
Why calendar units need their own type
Months do not all have the same length. February has 28 or 29 days, and July has 31. A year can have 365 or 366 days. So "one month" cannot be turned into a fixed number of days. A Period stores years, months, and days as three separate numbers. It works out their real length only when you add it to a specific date.
Creating a Period
You can build a Period with factory methods. A factory method is a static method that creates an object for you.
import java.time.LocalDate;
import java.time.Period;
public class Periods {
public static void main(String[] args) {
Period trial = Period.ofDays(30);
Period contract = Period.of(1, 6, 0); // 1 year, 6 months, 0 days
System.out.println(trial); // P30D
System.out.println(contract); // P1Y6M
System.out.println(Period.ofWeeks(2)); // P14D
LocalDate start = LocalDate.of(2024, 1, 31);
System.out.println(start.plus(Period.ofMonths(1))); // 2024-02-29
System.out.println(start.plus(contract)); // 2025-07-31
}
}The printed form follows the ISO 8601 standard. P means "period," then come the years (Y), months (M), and days (D). Weeks are stored as days, so two weeks prints as P14D.
When you add one month to January 31, you get February 29. February 2024 has no 31st day, so Java uses the last valid day of that month.
Finding the Period between two dates
Period.between(start, end) tells you how far apart two dates are, in years, months, and days. Each part has its own getter method.
import java.time.LocalDate;
import java.time.Period;
public class Age {
public static void main(String[] args) {
LocalDate birthday = LocalDate.of(1990, 5, 20);
LocalDate today = LocalDate.of(2024, 11, 5);
Period age = Period.between(birthday, today);
System.out.println(age); // P34Y5M16D
System.out.println(age.getYears()); // 34
System.out.println(age.getMonths()); // 5
System.out.println(age.getDays()); // 16
}
}Java counts whole years first, then whole months, then the days that are left over.
Normalizing
Java keeps a Period exactly as you built it. Period.ofMonths(14) prints P14M. To turn 12 of those months into a year, call normalized(), which returns P1Y2M. Days are never folded into months, because months have different lengths.
Exercise
What do Period.between(LocalDate.of(2024, 1, 1), LocalDate.of(2024, 3, 15)) and its getDays() return?
Show answer
The period is P2M14D. getDays() returns 14, the leftover days after two whole months. It is not 74, the total number of days between the dates.
Exercise
What date does LocalDate.of(2023, 3, 31).plus(Period.ofMonths(1)) produce, and why?
Show answer
It produces 2023-04-30. April has no 31st day, so Java uses the last valid day of April.