Skip to Content
Java Coding Problems
book

Java Coding Problems

by Anghel Leonard
September 2019
Intermediate to advanced
816 pages
18h 47m
English
Packt Publishing
Content preview from Java Coding Problems

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.

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Java Coding Problems - Second Edition

Java Coding Problems - Second Edition

Anghel Leonard
Java in a Nutshell, 7th Edition

Java in a Nutshell, 7th Edition

Benjamin J. Evans, David Flanagan
The Well-Grounded Java Developer, Second Edition

The Well-Grounded Java Developer, Second Edition

Benjamin Evans, Martijn Verburg, Jason Clark

Publisher Resources

ISBN: 9781789801415Supplemental Content