How to compare two Dates in Java.

This code snippet shows you how to compare a Date to another in Java.

In order to do this check, we use the compareTo operator of the java.util.Date class.
The java.util.Date.compareTo(Date anotherDate) compares a Date Object with the specified Date value.

The default usage is

where is one of these six comparison operators: [ <, ==, >, >=, !=, <= ]. x.compareTo(y) returns:

  • 0 if x and y are equal
  • 1 if x is greater than y
  • -1 if x is smaller than y

Ok, lets make some comparisons:

Check if a Java Date is bigger than another Date

Check if a Java Date is equal to another Date

See also

Next to the compareTo there are some other interesting methods which can be used to compare dates:

  • java.util.Date.before(Date when)
  • java.util.Date.after(Date when)


In this article we saw how we can use the java.util.Date.compareTo operator to compare a Date to another one.

%d bloggers like this: