Is there a way to have a Java8 duration of one year that accounts for leap years?

As per the response in Getting Duration using the new dateTime API you should be using

Period p = Period.ofYears(1);

It’s important to understand the difference between Duration (exact number of nanoseconds < 1 day) and Period (variable > 1 day).

Duration won’t account for leap days, daylight savings time or leap seconds, for example, and is intended for durations of less than a day, at most a few days.

So you should use Period instead.

Because different years have different number of days, if you want to find the number of days in a year, you need to specify which year you’re talking about.

If you want the number of days in a specific year, you can use

Year.of(year).length()

If you want the date one year from now, you can use

LocalDate.now().plusYears(1)

or

LocalDate.now().plus(Period.ofYears(1))

If you need the number of days between two dates, you can use

ChronoUnit.DAYS.between(start, end)

So to find the number of days to the date a year from now, you can use

LocalDate today = LocalDate.now();
long days = ChronoUnit.DAYS.between(today, today.plusYears(1));

If you want to see whether a membership of one year is still valid, you can use

Period membershipLength = Period.ofYears(1);
LocalDate membershipStart = ...;
LocalDate membershipEnd = membershipStart.plus(membershipLength);

LocalDate today = LocalDate.now();
boolean memberShipEnded = today.isAfter(membershipEnd);
boolean membershipValid = !membershipEnded;

Leave a Comment