Java – How to compare 2 Date instances using Date.compareTo() method ?

In this article, we will discuss how to compare 2 Date instances using compareTo() method

1. Date.compareTo() method :

  • Date.compareTo() method introduced in Java 1.2 version for java.util.Date comparisons
  • This method compares 2 Dates (java.util.Date) and returns integer number, if it
    1. returns positive number or 1, then invoking Date is greater/future Date with Comparing Date
    2. returns negative number or -1, then invoking Date is lesser/past Date with Comparing Date
    3. returns zero or 0, then invoking Date is equal/same Date with Comparing Date

2. Date.compareTo() example :

  • In the below illustration, we will create 3 different Dates (past/current/future) for comparison
    1. 1st compare, past date with current & future dates which will return -1 for both comparison
    2. 2nd compare, current date with past & future dates which will return -1 & 1 for comparison respectively
    3. 3rd compare, future date with past & current dates which will return 1 for both comparison

Compare2Dates.java

package in.bench.resources.date.comparison;

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

public class Compare2Dates {

	// main() method
	public static void main(String[] args) {

		// DateFormat & SimpleDateFormat - for formatting
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");


		// 2 different dates in string format
		String date1Str = "2021-12-31"; // past date in String
		String date2Str = "2022-01-11"; // current date in String
		String date3Str = "2022-09-22"; // future date in String


		try {

			// convert date in String to Date format
			Date date1Past = dateFormat.parse(date1Str); // past date
			Date date2Current = dateFormat.parse(date2Str); // current date
			Date date3Future = dateFormat.parse(date3Str); // future date


			// 1. compare past date with current date
			compareDates(date1Past, date2Current);

			// 2. compare past date with future date
			compareDates(date1Past, date3Future);


			// 3. compare current date with future date
			compareDates(date2Current, date3Future);

			// 4. compare current date with past date
			compareDates(date2Current, date1Past);


			// 5. compare future date with past date
			compareDates(date3Future, date1Past);

			// 6. compare future date with current date
			compareDates(date3Future, date2Current);

		} catch (ParseException e) {
			e.printStackTrace();
		}
	}


	/**
	 * This method compares 2 Dates (java.util.Date) and returns integer number, if it
	 * 
	 * - returns positive number or 1 -> then invoking Date is greater/future Date with Comparing Date
	 * - returns negative number or -1 -> then invoking Date is lesser/past Date with Comparing Date
	 * - returns zero or 0 -> then invoking Date is equal/same Date with Comparing Date
	 * 
	 * @param date1
	 * @param date2
	 */
	private static void compareDates(Date date1, Date date2) {

		// compare both dates
		int compare = date1.compareTo(date2);


		// print accordingly
		if(compare > 0) { // invoking date is future Date

			System.out.println("(" + date1 + ") is future date when comparing with (" + date2 + ")");

		} else if (compare < 0) { // invoking date is past Date

			System.out.println("(" + date1 + ") is past date when comparing with (" + date2 + ")");

		} else { // invoking date is same Date

			System.out.println("Both " +  date1 + " and " + date2 + " are same !!");
		}
	}
}

Output :

(Fri Dec 31 00:00:00 IST 2021) is past date when comparing with (Tue Jan 11 00:00:00 IST 2022)
(Fri Dec 31 00:00:00 IST 2021) is past date when comparing with (Thu Sep 22 00:00:00 IST 2022)
(Tue Jan 11 00:00:00 IST 2022) is past date when comparing with (Thu Sep 22 00:00:00 IST 2022)
(Tue Jan 11 00:00:00 IST 2022) is future date when comparing with (Fri Dec 31 00:00:00 IST 2021)
(Thu Sep 22 00:00:00 IST 2022) is future date when comparing with (Fri Dec 31 00:00:00 IST 2021)
(Thu Sep 22 00:00:00 IST 2022) is future date when comparing with (Tue Jan 11 00:00:00 IST 2022)

Related Articles :

References :

Happy Coding !!
Happy Learning !!

Java – How to convert java.util.Date to java.sql.Date and vice-versa ?
Java 8 – How to check whether a given Date is weekend ?