Using java.time.Period for Date Differences in Java


Period is used to model a quantity or amount of time in terms of years, months, and days, ideal for calculating differences between dates.

Source Code

LocalDate start = LocalDate.of(2023, 1, 1);
LocalDate end = LocalDate.of(2023, 12, 31);
Period period = Period.between(start, end);
System.out.println("Period: " + period.getYears() + " Year(s) " 
                   + period.getMonths() + " Month(s) " 
                   + period.getDays() + " Day(s)");
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments