Java:
Comparing two dates

How to:

Java makes life pretty easy when comparing dates. Use LocalDate and compareTo, isBefore, or isAfter methods. Here’s the skinny:

import java.time.LocalDate;

public class DateComparison {
    public static void main(String[] args) {
        LocalDate date1 = LocalDate.of(2023, 4, 1);
        LocalDate date2 = LocalDate.now(); // assuming today is 2023-4-15

        // Using compareTo
        int comparisonResult = date1.compareTo(date2);
        if(comparisonResult < 0) {
            System.out.println("Date1 is before Date2");
        } else if (comparisonResult > 0) {
            System.out.println("Date1 is after Date2");
        } else {
            System.out.println("Date1 is same as Date2");
        }

        // Using isBefore and isAfter
        if(date1.isBefore(date2)) {
            System.out.println("Date1 is earlier than Date2");
        } else if(date1.isAfter(date2)) {
            System.out.println("Date1 is later than Date2");
        } else {
            System.out.println("Date1 is the same day as Date2");
        }
    }
}

Sample output for today’s date as 2023-04-15:

Date1 is before Date2
Date1 is earlier than Date2

Deep Dive

Historically, Java’s date handling was, well, a headache. But then came Java 8 with java.time, a game-changer. Now we use LocalDate for dates sans time. Wanna compare dates including time? Look to LocalDateTime.

Alternatives? Sure. Before Java 8, there was java.util.Date and java.util.Calendar. You could still use them, but why dig your own grave?

Implementation-wise, compareTo returns int: negative if calling object is less (before), zero if equal, positive if greater (after). isBefore and isAfter return boolean. Easy to grasp, and no gotchas.

See Also

For more details dive into these: