Java
Programming
Date Comparison
Coding Tutorial
Software Development

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.

Browse interview questions

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.

  • 'LocalDate for a date without time or zone'
  • 'LocalDateTime for date and clock time without zone'
  • 'ZonedDateTime for date and time in a specific zone'
  • 'Instant for 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.

java
1import java.time.LocalDate;
2
3public class Main {
4    public static void main(String[] args) {
5        LocalDate start = LocalDate.of(2025, 1, 10);
6        LocalDate end = LocalDate.of(2025, 1, 20);
7
8        System.out.println(start.isBefore(end));
9        System.out.println(end.isAfter(start));
10        System.out.println(start.isEqual(end));
11    }
12}

These methods are clearer than comparing raw integers or timestamps.

You can also use compareTo when sorting or implementing ordering logic.

java
int result = start.compareTo(end);

A negative value means start is earlier.

Comparing Exact Instants

If you care about the actual moment in time, use Instant.

java
1import java.time.Instant;
2
3public class Main {
4    public static void main(String[] args) {
5        Instant a = Instant.parse("2025-01-10T10:15:30Z");
6        Instant b = Instant.parse("2025-01-10T10:15:31Z");
7
8        System.out.println(a.isBefore(b));
9    }
10}

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.

java
1import java.time.ZoneId;
2import java.time.ZonedDateTime;
3
4public class Main {
5    public static void main(String[] args) {
6        ZonedDateTime toronto = ZonedDateTime.of(2025, 1, 10, 9, 0, 0, 0, ZoneId.of("America/Toronto"));
7        ZonedDateTime london = ZonedDateTime.of(2025, 1, 10, 14, 0, 0, 0, ZoneId.of("Europe/London"));
8
9        System.out.println(toronto.toInstant().equals(london.toInstant()));
10    }
11}

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.

java
1import java.time.LocalDate;
2import java.time.temporal.ChronoUnit;
3
4public class Main {
5    public static void main(String[] args) {
6        LocalDate a = LocalDate.of(2025, 1, 10);
7        LocalDate b = LocalDate.of(2025, 1, 20);
8
9        long days = ChronoUnit.DAYS.between(a, b);
10        System.out.println(days);
11    }
12}

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.

java
1import java.time.Instant;
2import java.util.Date;
3
4Date oldDate = new Date();
5Instant instant = oldDate.toInstant();

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.time classes over legacy Date and Calendar.
  • Use LocalDate for date-only comparison and Instant for exact moments.
  • Use isBefore, isAfter, isEqual, or compareTo depending on intent.
  • Convert zoned values to Instant when comparing real timeline equality.
  • Pick the temporal type that matches the business meaning before writing comparison logic.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track 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.

Browse interview questions

All Rights Reserved.