issue with determining year difference using ChronoUnit in Java

12 hours ago 1
ARTICLE AD BOX

I want to determine the difference in year between two dates (e.g. from 2023-11-13 to 2024-11-12, which are 366 days) in Java with codes shown below.

long differenceInYear = ChronoUnit.YEARS.between(LocalDate.of(2023, 11, 13), LocalDate.of(2024, 11, 12));

The result is 0 but I expect the result to be 1.

Then I try to determine the difference in month and day respectively with codes shown below.

long differenceInMonth = ChronoUnit.MONTHS.between(LocalDate.of(2023, 11, 13), LocalDate.of(2024, 11, 12)); long differenceInDay = ChronoUnit.DAYS.between(LocalDate.of(2023, 11, 13), LocalDate.of(2024, 11, 12));

differenceInMonth is 11 but I expect it to be 12 while differenceInDay is 365 but I expect it to be 366 since 2024 is a leap year.

Why is differenceInYear not 1, differenceInMonth not 12 and differenceInDay not 365?

Read Entire Article