Javatpoint Logo
Javatpoint Logo

How to Compare Dates in Java

In Java, while we deal with date and time, sometimes we need to compare dates. The comparison of dates in Java is not the same as the comparison of two numbers. So, it is a little bit tricky task to compare two dates in Java. We need not to implement any logic to compare dates. To make this task easy Java provides compareTo(), before(), after(), and equals() method. In this section, we are going to learn how to compare two dates in Java.

There is four class in Java that provides methods to compare two dates.

  • Using compareTo() Method
  • Using Date Class
  • Using Calendar Class
  • Using LocalDate Class

Using Date.compareTo() Method

Java Date class provides different methods related to time and dates. It is a class of java.util package. The class implements the Serializable, Cloneable, and Comparable<Date> interfaces.

For comparison of two dates, the class provides compareTo() method. It compares Dates for ordering. It parses a date (to be compared) as a parameter. It throws NullPointerException if the argument date is null.

Syntax:

It returns integer values:

  • 0: if both dates are equal.
  • A value less than 0: if the date is before the argument date.
  • A value greater than 0: if the date is after the argument date.

Remember: If you are dealing with date in Java, do not forget to import java.text.SimpleDateFormat, java.text.ParseException, java.util.Date.

Let's implements the compareTo() method and compare two dates.

In the following example, we have created an instance of the SimpleDateFormat class that allows us to take different date formats. After that, we have taken two variables date1 and date2 of type Date. By using the parse() method of the SimpleDateFormat class, we have parsed the dates to compare. The method returns a date parsed from the string. We have passed the date1 and date2 variable of type Date in the format() method. The method gives the formatted date/ time string.

For comparing the two dates, we have used the compareTo() method. If both dates are equal it prints Both dates are equal. If date1 is greater than date2, it prints Date 1 comes after Date 2. If date1 is smaller than date2, it prints Date 1 comes after Date 2.

CompareDatesExample1.java

Output:

Date 1: 2020-07-20
Date 2: 2020-06-18
Date 1 comes after Date 2

Using Date Class

Java date class provides before(), after(), and equals() method to compare two dates.

before(): The method check that the date comes before the specified date or not. It parses a parameter of type Date. It 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.

Syntax:

It throws NullPointerException if when is null.

after(): The method check that the date comes after the specified date or not. It parses a parameter of type Date. It returns true if and only if the instant of time represented by this Date object is strictly later than the instant represented by when, false otherwise.

Syntax:

It throws NullPointerException if when is null.

equals(): The method checks (compare) the equality of two dates. It overrides the equals() method of the Object class. It returns true if the objects are same, else returns false. Therefore, the Date objects will be equal if and only if the getTime() method returns the same long value for both dates.

Syntax:

Let's use the above-explained method in an example and compare two dates with the help of these methods.

CompareDatesExample2.java

Output:

Date1: 2019-01-01
Date2: 2020-01-01
Date1 comes before Date2

Using Calendar Class

Like the Java Date class, the Calendar class also provides before(), after(), and equals() methods. All three methods have the same signature, as we have explained above.

Let's use the Calendar class and compare two dates with the help of after(), before(), and equals() method.

In the following example, we have used the same method used in the previous example, except the getInstance() and setTime() methods.

getInstance(): It is a static method of the Calendar. It returns a Calendar using the default time zone and locale.

Syntax:

setTime(): The method sets the calendar time according to the specified date. It parses a parameter of type Date.

Syntax:

CompareDatesExample3.java

Output:

Date1: 2020-12-01
Date2: 2020-12-01
Both dates are equal

Using LocalDate Class

Java provides another LocalDate class to compare two LocalDate, LocalTime, and LocalDateTime. It is the member of java.time package. The class provides isBefore(), isAfter(), isEquals(), and compareTo() method to compare dates. These methods works same as the method before(), after(), and equals() of the Date and Calendar class.

Let's use the LocalDate class in an example to compare two dates.

In the following example, we have used the following method to compare two dates. All the methods check the dates according to the local-time line.

of(): It is a static method of LocalDate class. It obtains an instance of LocalDate form year, month, and day. It accepts three parameters year, month, and date of type int. It returns a LocalDate with the specified date.

Syntax:

where:

year: must be between MIN_YEAR to MAX_YEAR.

month: must be between 1 (January) to 12 (December).

datOfMonth: must be between 1 to 31.

It throws DateTimeException if the value of any parameter is out of range.

isBefore(): The method checks the date is before the specified date. It parses a date (to compare) as a parameter. It returns true if and only if the date is before the specified date. Its comparison approach is different from compareTo(ChronoLocalDate).

Syntax:

isAfter(): The method checks the date is before the specified date. It parses a date (to compare) as a parameter. It returns true if and only if the date is before the specified date. Its comparison approach is different from compareTo(ChronoLocalDate).

Syntax:

isEqual(): The method compares the dates are equal or not. If both dates are equal it returns true, false otherwise. It parses a date (to compare) as a parameter.

It returns true if and only if the date is before the specified date. Its comparison approach is different from compareTo(ChronoLocalDate).

Syntax:

CompareDatesExample4.java

Output:

Date1: 2020-09-29
Date2: 2020-12-07
Date1 comes before Date2






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA