September 2019
Intermediate to advanced
816 pages
18h 47m
English
A better idea is to upgrade to JDK 8, and rely on the following straightforward snippet of code:
LocalDate startLocalDate = LocalDate.of(1977, 11, 2);LocalDate endLocalDate = LocalDate.now();long years = ChronoUnit.YEARS.between(startLocalDate, endLocalDate);
Adding months and days to the result is also easy to accomplish, thanks to the Period class:
Period periodBetween = Period.between(startLocalDate, endLocalDate);
Now, the age in years, months, and days can be obtained via periodBetween.getYears(), periodBetween.getMonths(), and periodBetween.getDays().
For example, between the current date, February 28, 2019, and November 2, 1977, we have 41 years, 3 months, and 26 days.