Luke Luke - 8 months ago 75
Java Question

Java Time period in decimal number of years

If I calculate the difference between 2

LocalDate
's in
java.time
using:

Period p = Period.between(testDate, today);


Then I get an output with the number of years, months, days like:

Days = 9
Months = 6
Years = 18


Does anyone know a clean way to represent that as a decimal type value (ie, above would be something around
18.5...
)?

VGR VGR
Answer Source

I would avoid using Period, and instead just calculate the difference in days:

float years = testDate1.until(today, ChronoUnit.DAYS) / 365.2425f;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download