Starting with JDK 8

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset