Calculate days between two Dates in Java 8
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Java 8, the normal way to calculate the number of days between two calendar dates is ChronoUnit.DAYS.between. The confusion usually starts when people mix LocalDate, LocalDateTime, Instant, and Period, which answer slightly different questions.
If you are comparing dates on the calendar and do not care about time-of-day, use LocalDate. That keeps the calculation simple and avoids time-zone surprises.
Use LocalDate With ChronoUnit.DAYS.between
For plain date math, this is the standard solution:
This prints 9, because the calculation is start-inclusive and end-exclusive. In other words, it counts how many day boundaries you cross to get from the first date to the second.
Understand Inclusive Versus Exclusive Counting
This is where many off-by-one mistakes come from:
- from
2024-01-01to2024-01-01is0 - from
2024-01-01to2024-01-02is1 - from
2024-01-01to2024-01-10is9
If your business rule is inclusive, such as "how many calendar dates are covered," then add one intentionally:
Do that only when the domain requires it. Do not add one just because the first result looked surprising.
Why Period Is Different
Period is useful, but it does not return a total day count:
Period breaks the gap into years, months, and days. period.getDays() is only the remaining day component after months and years are separated out. It is not the same as the total number of days between the two dates.
If the question is "how many days total," ChronoUnit.DAYS.between is usually the right answer.
Converting Legacy Date Values
Older code often still uses java.util.Date. Convert those values into the Java 8 time API first:
This is safer than subtracting raw milliseconds because you are working at the correct calendar level.
When Time of Day Matters
If you actually care about elapsed time rather than calendar dates, LocalDate may be the wrong type. For example, the difference between 2024-01-01T23:00 and 2024-01-02T01:00 is only two hours, even though the dates differ.
In that case, use Instant, ZonedDateTime, or LocalDateTime depending on the problem. The data type should match the meaning of the calculation.
Negative Results Are Useful
If the second date is before the first, ChronoUnit.DAYS.between returns a negative value:
That is usually a good thing because it preserves interval direction. If your use case wants absolute distance, apply Math.abs(days) explicitly.
Common Pitfalls
The most common mistake is using Period.getDays() and assuming it means "total days between these two dates." It does not.
Another frequent issue is subtracting milliseconds from legacy Date objects and forgetting that the real business question is often about calendar dates, not elapsed clock time.
Developers also trip over inclusive-versus-exclusive rules. ChronoUnit.DAYS.between is consistent, but you still have to decide whether your business rule matches that convention.
Finally, choose the time type carefully. If you are solving a calendar-date problem, use LocalDate. If you are solving an elapsed-time problem, use a type that includes time.
Summary
- Use
ChronoUnit.DAYS.betweenwithLocalDatefor calendar-date differences in Java 8. - Remember that the calculation is start-inclusive and end-exclusive.
- Do not use
Period.getDays()as a substitute for total day count. - Convert legacy
Datevalues into the Java 8 time API before doing date math. - Match the time type to the real question: calendar dates versus elapsed time.

