W3docs

How to compare dates in Java?

You can compare dates in Java by using the compareTo() method of the java.util.Date class. This method compares the date object on which it is called with the date object passed as an argument to the method

You can compare dates in Java by using the compareTo() method of the java.util.Date class. This method compares the date object on which it is called with the date object passed as the argument, and returns an integer value indicating whether the date is before, after, or the same as the argument date.

Here is an example of how you can use compareTo() to compare two dates in Java:


import java.util.Date;

public class DateComparisonExample {
  public static void main(String[] args) {
    // Note: Date(year, month, day) is deprecated. Year is years since 1900.
    Date date1 = new Date(123, 0, 1); // Jan 1, 2023
    Date date2 = new Date(123, 0, 2); // Jan 2, 2023

    // Compare date1 and date2
    int result = date1.compareTo(date2);
    if (result == 0) {
      System.out.println("date1 and date2 are the same");
    } else if (result < 0) {
      System.out.println("date1 is before date2");
    } else {
      System.out.println("date1 is after date2");
    }
  }
}

If you want to compare dates without taking into account the time portion of the date, you can use the java.util.Calendar class to set the time portion of the dates to be the same before performing the comparison. Note that Calendar.compareTo() compares the full timestamp, including hours, minutes, seconds, and milliseconds. To compare only the date portion, you must zero out the time fields first. Additionally, Calendar uses the system's default timezone, which can cause unexpected results if timezones differ. For consistent comparisons, explicitly set the timezone using Calendar.getInstance(TimeZone.getTimeZone("UTC")). Here is an example of how you can do this:


import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class DateComparisonExample {
  public static void main(String[] args) {
    // Use UTC to avoid default timezone differences
    Calendar cal1 = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
    Calendar cal2 = Calendar.getInstance(TimeZone.getTimeZone("UTC"));

    // Set both calendars to be at midnight
    cal1.set(Calendar.HOUR_OF_DAY, 0);
    cal1.set(Calendar.MINUTE, 0);
    cal1.set(Calendar.SECOND, 0);
    cal1.set(Calendar.MILLISECOND, 0);
    cal2.set(Calendar.HOUR_OF_DAY, 0);
    cal2.set(Calendar.MINUTE, 0);
    cal2.set(Calendar.SECOND, 0);
    cal2.set(Calendar.MILLISECOND, 0);

    // Compare the two calendars
    int result = cal1.compareTo(cal2);
    if (result == 0) {
      System.out.println("cal1 and cal2 are the same");
    } else if (result < 0) {
      System.out.println("cal1 is before cal2");
    } else {
      System.out.println("cal1 is after cal2");
    }
  }
}

For modern Java applications (Java 8+), it is recommended to use the java.time API instead of the legacy java.util.Date and java.util.Calendar classes. The java.time classes provide clearer, type-safe methods for comparison, such as isBefore(), isAfter(), isEqual(), and compareTo().

Here is an example using LocalDate:


import java.time.LocalDate;

public class ModernDateComparison {
  public static void main(String[] args) {
    LocalDate date1 = LocalDate.of(2023, 1, 1);
    LocalDate date2 = LocalDate.of(2023, 1, 2);

    if (date1.isEqual(date2)) {
      System.out.println("date1 and date2 are the same");
    } else if (date1.isBefore(date2)) {
      System.out.println("date1 is before date2");
    } else {
      System.out.println("date1 is after date2");
    }
  }
}

For date-time comparisons, LocalDateTime works similarly and includes time precision without timezone ambiguity.


import java.time.LocalDateTime;

public class ModernDateTimeComparison {
  public static void main(String[] args) {
    LocalDateTime dateTime1 = LocalDateTime.of(2023, 1, 1, 10, 30);
    LocalDateTime dateTime2 = LocalDateTime.of(2023, 1, 1, 14, 45);

    if (dateTime1.isEqual(dateTime2)) {
      System.out.println("dateTime1 and dateTime2 are the same");
    } else if (dateTime1.isBefore(dateTime2)) {
      System.out.println("dateTime1 is before dateTime2");
    } else {
      System.out.println("dateTime1 is after dateTime2");
    }
  }
}