Compare Dates in Java

In this post there are Java programs to compare dates in Java, options you have are using Date class methods, Calendar class methods and from Java 8 using methods in LocalDate, LocalTime and LocalDateTime classes.

Comparing java.util.Date

If you have two Date instances and you want to compare them then the methods in the Date class that can be used are-

  • compareTo(Date anotherDate)– Compares two Dates for ordering. Returns 0 if the argument Date is equal to this Date; a value less than 0 if this Date is before the passed Date argument; and a value greater than 0 if this Date is after the Date argument.
  • equals(Object obj)– Compares two dates for equality. The result is true if and only if the argument is not null and is a Date object that represents the same point in time, to the millisecond, as this object.
  • after(Date when)– Tests if this date is after the specified date. Returns true if the instant represented by this Date object is strictly later than the instant represented by when; false otherwise.
  • before(Date when)– Tests if this date is before the specified date. Returns true if and only if the instant of time represented by this Date object is strictly earlier than the instant represented by when; false otherwise.
public class CompareDates {
  public static void main(String[] args) throws ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date dt1 = sdf.parse("2019-10-27");
    Date dt2 = sdf.parse("2019-08-20");
    System.out.println("Date1 is- "+ sdf.format(dt1));
    System.out.println("Date2 is- "+ sdf.format(dt2));
    compareDates(dt1, dt2);
  }
	
  private static void compareDates(Date dt1, Date dt2) {
    if(dt1.compareTo(dt2) > 0) {
      System.out.println("Date1 comes after date2");
    }else if(dt1.compareTo(dt2) < 0) {
      System.out.println("Date1 comes before date2");
    }else {
      System.out.println("Date1 equals date2");
    }
		
    // Using after method
    if(dt1.after(dt2)) {
      System.out.println("Date1 comes after date2");
    }else {
      System.out.println("Date1 comes before date2");
    }
		
    // Using before method
    if(dt1.before(dt2)) {
      System.out.println("Date1 comes before date2");
    }else {
      System.out.println("Date1 comes after date2");
    }
		
    //using equals method
    if(dt1.equals(dt2)) {
      System.out.println("Date1 equals date2");
    }else {
      System.out.println("Date1 is not equal to date2");
    }
  }
}

Output

Date1 is- 2019-10-27
Date2 is- 2019-08-20
Date1 comes after date2
Date1 comes after date2
Date1 comes after date2
Date1 is not equal to date2

Comparing java.util.Calendar

If you have Calendar instances then you can compare them the same way Date instances are compared. In Calendar class also there are similar methods compareTo, equals, after, before.

public class CompareDates {

	public static void main(String[] args) throws ParseException {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date dt1 = sdf.parse("2018-09-27");
		Date dt2 = sdf.parse("2019-08-20");
		Calendar cal1 = Calendar.getInstance();
		Calendar cal2 = Calendar.getInstance();
		cal1.setTime(dt1);
		cal2.setTime(dt2);
		System.out.println("Date1 is- "+ sdf.format(cal1.getTime()));
		System.out.println("Date2 is- "+ sdf.format(cal2.getTime()));
		compareDates(cal1, cal2);
	}
	
	// Comparing Calendar instances
	private static void compareDates(Calendar cal1, Calendar cal2) {
		if(cal1.compareTo(cal2) > 0) {
			System.out.println("Date1 comes after date2");
		}else if(cal1.compareTo(cal2) < 0) {
			System.out.println("Date1 comes before date2");
		}else {
			System.out.println("Date1 equals date2");
		}
		
		// Using after method
		if(cal1.after(cal2)) {
			System.out.println("Date1 comes after date2");
		}else {
			System.out.println("Date1 comes before date2");
		}
		
		// Using before method
		if(cal1.before(cal2)) {
			System.out.println("Date1 comes before date2");
		}else {
			System.out.println("Date1 comes after date2");
		}
		
		//using equals method
		if(cal1.equals(cal2)) {
			System.out.println("Date1 equals date2");
		}else {
			System.out.println("Date1 is not equal to date2");
		}
	}
}

Output

Date1 is- 2018-09-27
Date2 is- 2019-08-20
Date1 comes before date2
Date1 comes before date2
Date1 comes before date2
Date1 is not equal to date2

Comparing LocalDates in Java

Java 8 onward you can use classes in new Date and Time API for comparing dates in Java. Here is an example using LocalDate instances. Similar methods are there in LocalTime and LocalDateTime classes too.

For comparing two LocalDate instances there are the following methods-

  • compareTo(ChronoLocalDate other)– Compares this date to another date. Returns the comparator value, negative if less, positive if greater.
  • isAfter(ChronoLocalDate other)– Checks if this date is after the specified date. Returns true if this date is after the specified date.
  • isBefore(ChronoLocalDate other)– Checks if this date is before the specified date. Returns true if this date is before the specified date.
  • isEqual(ChronoLocalDate other)– Checks if this date is equal to the specified date. Returns true if this date is equal to the specified date.
public class CompareDates {

	public static void main(String[] args) {
		LocalDate ld1 = LocalDate.of(2019, Month.OCTOBER, 18);
		LocalDate ld2 = LocalDate.of(2019, Month.SEPTEMBER, 20);
		System.out.println(ld1.compareTo(ld2));
		System.out.println(ld2.compareTo(ld1));
		
		System.out.println(ld1.isAfter(ld2));
		System.out.println(ld1.isBefore(ld2));
		System.out.println(ld1.isEqual(ld2));
	}
}

Output

1
-1
true
false
false

Related Posts

That’s all for the topic Compare Dates in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.