Java Programs›Date & Time›Compare Two Dates

Compare Two Dates in Java

beginner·  Date & Time  ·  Date & Time

Problem

Dates have a natural order on the calendar, so two LocalDate values can be compared to see which comes first.

Given a deadline of 30 June 2026 and a submission on 2 July 2026, decide whether the submission was on time.

Input
deadline 2026-06-30, submitted 2026-07-02
Output
Status: late

Java Program

Java
import java.time.LocalDate; public class CompareDates { public static void main(String[] args) { LocalDate deadline = LocalDate.of(2026, 6, 30); LocalDate submitted = LocalDate.of(2026, 7, 2); System.out.println("Submitted before deadline? " + submitted.isBefore(deadline)); System.out.println("Submitted after deadline? " + submitted.isAfter(deadline)); System.out.println("Same day? " + submitted.isEqual(deadline)); System.out.println("compareTo result: " + submitted.compareTo(deadline)); // > 0 means later if (submitted.isAfter(deadline)) { System.out.println("Status: late"); } else { System.out.println("Status: on time"); } } }

Output

Submitted before deadline? false Submitted after deadline? true Same day? false compareTo result: 1 Status: late

Core Logic

LocalDate provides readable comparison methods that each return a boolean, plus compareTo for sorting.

How It Works
  1. 1submitted.isBefore(deadline) is false and isAfter is true.
  2. 2isEqual checks for the same calendar day.
  3. 3compareTo returns a negative number, zero or a positive number, like any Comparable.
  4. 4An if on isAfter decides the final status.
2 July is two days after 30 June, so the submission is reported as late.
💡

Key Point: Only rely on the sign of compareTo, not its exact value — the spec promises negative, zero or positive, nothing more.

Key Concepts

isBefore()isAfter()isEqual()compareTo()

Approach 2: Sorting a List of Dates

Java
import java.time.LocalDate; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; public class SortDates { public static void main(String[] args) { List<LocalDate> dates = new ArrayList<>(Arrays.asList( LocalDate.of(2026, 7, 2), LocalDate.of(2025, 12, 25), LocalDate.of(2026, 6, 30), LocalDate.of(2026, 1, 1))); Collections.sort(dates); // LocalDate implements Comparable, oldest first System.out.println("Oldest first: " + dates); dates.sort(Collections.reverseOrder()); System.out.println("Newest first: " + dates); System.out.println("Latest date: " + Collections.max(dates)); } }

Output

Oldest first: [2025-12-25, 2026-01-01, 2026-06-30, 2026-07-02] Newest first: [2026-07-02, 2026-06-30, 2026-01-01, 2025-12-25] Latest date: 2026-07-02

Core Logic

Because LocalDate implements Comparable, the standard collection utilities can sort dates and find extremes directly.

How It Works
  1. 1Collections.sort(dates) orders the dates oldest first.
  2. 2dates.sort(Collections.reverseOrder()) flips them to newest first.
  3. 3Collections.max returns the latest date.
Christmas 2025 sorts before New Year 2026, even though it was added second.
💡

Key Point: Never sort dates as strings in day-first formats such as 02/07/2026 — text order and calendar order disagree; keep them as LocalDate until display.

Key Concepts

Collections.sort()Collections.reverseOrder()Collections.max()

Interview Questions

Should you use equals or isEqual?

For two LocalDate values they agree. For ZonedDateTime values, equals also compares the zone, while isEqual compares only the instant.

How do you check whether a date is in a range?

!date.isBefore(start) && !date.isAfter(end) includes both ends of the range.

Learn the Concept

Related Programs