How to compare two dates in Java?



In Java,we can compare two dates using the compareTo() method of the Comparable interface. This is a common way to compare two dates, and this method returns an integer value based on which you can specify which date occurs before, after, or if it is equal.

Here are the return values by this method, based on which you verify the date appearance:

Compare Dates Result
date1.compareTo(date2) > 0 date1 occurs after date2
date1.compareTo(date2) < 0 date1 occurs before date2
date1.compareTo(date2) == 0 date1 are date2 are equal

Comparing Dates in Java

Below are various approaches to compare two dates in Java:

Using CompareTo() Method

The compareTo() method of the Comparable interface is used to compare the this (current) object with the specified object for order.

It returns a negative integer (-ve), zero, or a positive (+ve) integer as this (i.e., current) object is less than, equal to, or greater than the specified object.

Syntax

Following is the syntax of the compareTo() method:

int compareTo(T o)

Here, o is an object to be compared.

Example

In the following example, we use the compareTo() method to compare dates "2019-04-15" and "2019-08-10". Since date1 occurs before date2, this method will return a negative value:

import java.text.*;
import java.util.Date;
public class CompareDates {
   public static void main(String[] args) throws ParseException {
      //date formatter
      SimpleDateFormat sdformat = new SimpleDateFormat("yyyy-MM-dd");
      
      Date d1 = sdformat.parse("2019-04-15");
      Date d2 = sdformat.parse("2019-08-10");
      
      System.out.println("The date1 is: " + sdformat.format(d1));
      System.out.println("The date2 is: " + sdformat.format(d2));
      
      //comparing dates using compareTo() method
      if(d1.compareTo(d2) > 0) {
         System.out.println("Date1 occurs after Date2");
      }
      else if(d1.compareTo(d2) < 0) {
         System.out.println("Date1 occurs before Date2");
      }
      else if(d1.compareTo(d2) == 0) {
         System.out.println("Both dates are equal");
      }
   }
}

The above example produces the following output:

The date1 is: 2019-04-15
The date2 is: 2019-08-10
Date1 occurs before Date2

Using Date.before(), Date.after() and Date.equals() Methods

In Java, the Date is a class that represents a specific instant in time, with millisecond precision.

It provides various comparison (or say testing) methods, such as before(), after(), and equal(), which test whether this (current) date occurs before, after, or equal to the specified date.

Syntax

Following are the syntaxes of the Date.before(), Date.after() and Date.equals() method:

public boolean after(Date d)
public boolean equals(Date d)
public boolean before(Object obj)

Here, d is a date to be compared, and obj is another Date instance that will be compared with the current Date instance.

Example

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class compareDates {
   public static void main (String[] args) throws ParseException {
      
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-mm-yyyy");
      
      //Date class
      Date date1 = simpleDateFormat.parse("15-05-2024");
      Date date2 = simpleDateFormat.parse("19-06-2025");
      
      System.out.println("The date1 is: " + date1);
      System.out.println("The date2 is: " + date2);
      
      //using calendar before(), equals() and after() method to compare the dates
      System.out.println("Does the date1 come before the date2? " + date1.before(date2));
      System.out.println("Is it the date1 is same as the date2? " + date1.equals(date2));
      System.out.println("Does the date1 come after the date2? " + date1.after(date2));
   }
}

Following is the output of the above program:

The date1 is: Mon Jan 15 00:05:00 IST 2024
The date2 is: Sun Jan 19 00:06:00 IST 2025
Does the date1 come before the date2? true
Is it the date1 is same as the date2? false
Does the date1 come after the date2? false

Using Calendar Class Methods

The Calendar class is an abstract class that provides various methods for converting and comparing a specific instant in time.

The before() and after() methods check whether the Calendar instance represents a time before or after the time represented by the specified object, respectively.

The equals() method compares the Calendar instance with the specified object. All these methods return a boolean value (true or false) based on the comparison.

Syntax

Following are the syntaxes of the Calendar.before(), Calendar.after(), and Calendar.equals() methods:

public boolean after(Object obj)
public boolean equals(Object obj)
public boolean before(Object obj)

Here, obj is another Calendar instance that will be compared with the current Calendar instance.

Example

The following program uses the before(), equals(), and after() methods of Calendar class to compare two dates whether they came before, or same, or after the time represented by a specified object:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

class compareDate {
   public static void main (String[] args) throws ParseException {
   
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-mm-yyyy");
      
      Date date1 = simpleDateFormat.parse("19-06-2025");
      Date date2 = simpleDateFormat.parse("15-05-2024");
      
      Calendar calendar1 = Calendar.getInstance();
      Calendar calendar2 = Calendar.getInstance();
      
      System.out.println("The date1 is: " + date1);
      System.out.println("The date2 is: " + date2);
      
      calendar1.setTime(date1);
      calendar2.setTime(date2);
      
      //using calendar before(), equals() and after() method to compare the dates
      System.out.println("Does the date1 come before the date2? " + calendar1.before(calendar2));
      System.out.println("Is it the date1 is same as the date2? " + calendar1.equals(calendar2));
      System.out.println("Does the date1 come after the date2? " + calendar1.after(calendar2));
   }
}

Below is the output of the above program:

The date1 is: Sun Jan 19 00:06:00 IST 2025
The date2 is: Mon Jan 15 00:05:00 IST 2024
Does the date1 come before the date2? false
Is it the date1 is same as the date2? false
Does the date1 come after the date2? true
Updated on: 2025-06-20T17:05:55+05:30

81K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements