How to compare dates in Java?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In modern Java, date comparison should usually be done with the java.time API, not the old java.util.Date constructors. The important question is not only how to compare two values, but also what kind of temporal value you are comparing: a calendar date, a local date-time, or an exact instant on the timeline.
Choose the Right Type First
Java has several different temporal classes, each for a different meaning.
- '
LocalDatefor a date without time or zone' - '
LocalDateTimefor date and clock time without zone' - '
ZonedDateTimefor date and time in a specific zone' - '
Instantfor an exact moment in UTC'
If you compare the wrong type, the code may compile and still express the wrong business rule.
Comparing LocalDate
For date-only logic, use isBefore, isAfter, or isEqual.
These methods are clearer than comparing raw integers or timestamps.
You can also use compareTo when sorting or implementing ordering logic.
A negative value means start is earlier.
Comparing Exact Instants
If you care about the actual moment in time, use Instant.
Instant is the safest choice when data comes from logs, APIs, or databases storing UTC timestamps.
Comparing Zoned Times Correctly
Two ZonedDateTime values may look different locally and still represent the same instant.
If the question is "same instant," compare toInstant(). If the question is "same local clock time," compare the zoned values differently.
Date Differences Are Not the Same as Comparison
Sometimes you do not just want before or after. You want the amount of time between values.
This is often cleaner than using Period when you specifically need total day counts.
Avoid Legacy Date Unless You Must Interoperate
java.util.Date and Calendar are older APIs. They still exist, but new code should prefer java.time. If you receive a legacy Date, convert it.
Do not use deprecated Date constructors such as new Date(2023, 1, 15). They are misleading because the year and month semantics are not what most developers expect.
Common Pitfalls
A common mistake is comparing formatted date strings instead of comparing temporal objects. String comparison depends on formatting and can be wrong unless the format is carefully chosen.
Another mistake is using LocalDateTime for cross-time-zone comparisons. It has no zone or offset, so it cannot represent a global instant by itself.
Developers also confuse "same date" with "same instant." Those are different questions and often require different Java types.
Summary
- Prefer
java.timeclasses over legacyDateandCalendar. - Use
LocalDatefor date-only comparison andInstantfor exact moments. - Use
isBefore,isAfter,isEqual, orcompareTodepending on intent. - Convert zoned values to
Instantwhen comparing real timeline equality. - Pick the temporal type that matches the business meaning before writing comparison logic.
Related reading
- How to compare the performance of Android Apps written in Java and Xamarin C#? Anyway to check quantitative data (code & results)
- How to compare the performance of Android Apps written in Java and Xamarin C? Anyway to check quantitative data code results
- How to compare two ListString to each other?
- how to compress data in producers when using spring kafka
- How to conditionally declare Bean when multiple profiles are not active?
- How to configure additional classpath in SpringBoot?
- How to configure CharacterEncodingFilter in SpringBoot?
- How to configure CORS in a Spring Boot Spring Security application?

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.