Java 8 Difference between two LocalDateTime in multiple units
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Java 8 gives you several ways to measure the gap between two LocalDateTime values, but each API answers a slightly different question. ChronoUnit is best for total elapsed units, Duration is good for time-based differences, and Period belongs to date-only calculations rather than full date-time values.
Start With What LocalDateTime Means
LocalDateTime contains date and time fields but no timezone or offset. That means the difference is calculated in local calendar terms, not in terms of real-world timezone transitions.
If daylight saving changes or cross-region timestamps matter, convert to ZonedDateTime or Instant first.
Use ChronoUnit.between for Total Units
When you want the total number of days, hours, minutes, or seconds between two values, ChronoUnit.between is the simplest tool.
The important detail is that each call gives a total unit count:
- days:
1 - hours:
26 - minutes:
1590
Those are not component parts. They are full elapsed totals.
Use Duration for Time-Based Breakdown
If you want to break the difference into multiple units in one calculation, Duration is often more convenient.
This example produces a component-style breakdown rather than unrelated total counts.
Period Is Not for Full LocalDateTime Differences
Period works with LocalDate, not LocalDateTime, and it measures date-based components such as years, months, and days. If you convert a LocalDateTime to LocalDate, you lose the time-of-day part.
Use this only when your requirement is genuinely calendar-based.
Getting Multiple Units Correctly
Developers often want an answer like "2 days, 3 hours, 10 minutes." The safest approach is:
- compute one duration
- extract total units
- compute the remainder for smaller units
Do not call ChronoUnit.DAYS.between, ChronoUnit.HOURS.between, and ChronoUnit.MINUTES.between and then treat those results as component parts. Those values overlap because each one is a total from the same start.
Negative Differences
If the end is before the start, ChronoUnit and Duration will return negative values.
Sometimes that is correct. Other times you may want Math.abs(...) or explicit input normalization before formatting the result.
Common Pitfalls
The biggest pitfall is mixing total units with component units. 26 total hours is not the same as 1 day and 2 hours, even though they describe the same span.
Another issue is using LocalDateTime when timezone-aware time is actually required. If an event crosses a daylight saving boundary, local date-time arithmetic may not reflect the real elapsed clock time you care about.
Developers also misuse Period for date-time calculations. Period is for date-based components, not for hour and minute differences.
Finally, avoid recomputing the same difference in many unrelated ways. Pick one representation, then derive the display you need from that representation.
Summary
- Use
ChronoUnit.betweenfor total elapsed units such as total hours or total minutes. - Use
Durationwhen you want time-based calculations and remainder-style breakdowns. - Use
Periodonly for date-based differences, not for fullLocalDateTimevalues. - Treat
LocalDateTimeas timezone-free local data, not as a real instant on the global timeline. - Decide whether you need total units or component parts before choosing the API.
Related reading
- Java 8 Distinct by property
- java 8 Docker Improperly specified VM option 'InitialRAMPercentageXX
- Java 8 forEach with index
- Java 8 How do I work with exception throwing methods in streams?
- Java 8 Iterable.forEach() vs foreach loop
- Java 8 Lambda-Streams, Filter by Method with Exception
- Java 8 Lambda function that throws exception?
- Java 8 lambda Void argument

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.