In this article, you'll learn how to compare two dates in Java using both Java 8 new date and time API and the legacy Date
and Calendar
API.
Java 8 Date & Time API
Java 8 introduced a brand new date and time API (classes in the java.time.*
package) to fix the flaws in the old API. The new API is thread-safe, easier to use, and provides utility methods for performing different date and time tasks.
Java 8 new date and time API provides the following methods to compare two instances of dates:
isAfter()
— Returntrue
if this date is after the specified date.isBefore()
— Returntrue
if this date is before the specified date.isEqual()
— Returntrue
if this date is equal to the specified date.compareTo()
— Returns the comparator value, negative if less, positive if greater.
Let us look at the examples that use the above methods to compare new date and time API instances.
Comparing two LocalDate
instances
An instance of LocalDate
represents a date in ISO-8601 format (yyyy-MM-dd) without time. Unlike the Date
, it doesn't store any time or timezone information.
The following example demonstrates how you can parse two strings to instances of LocalDate
and then compare them to see if they before, equal, or after each other:
LocalDate date1 = LocalDate.parse("2018-04-12");
LocalDate date2 = LocalDate.parse("2019-07-17");
// compare `LocalDate` instances
System.out.println(date1 + " is BEFORE " + date2 + ": " + date1.isBefore(date2));
System.out.println(date1 + " is AFTER " + date2 + ": " + date1.isAfter(date2));
System.out.println(date1 + " is EQUAL to " + date2 + ": " + date1.isEqual(date2));
The above code will print the following on the console:
2018-04-12 is BEFORE 2019-07-17: true
2018-04-12 is AFTER 2019-07-17: false
2018-04-12 is EQUAL to 2019-07-17: false
Let us look at another example that uses compareTo()
to compare two instances of LocalDate
:
LocalDate date1 = LocalDate.of(2018, Month.JULY, 12);
LocalDate date2 = LocalDate.of(2016, Month.AUGUST, 25);
// compare dates
int diff = date1.compareTo(date2);
if (diff == 0) {
System.out.println(date1 + " is EQUAL to " + date2);
} else if (diff > 0) {
System.out.println(date1 + " is AFTER " + date2);
} else {
System.out.println(date1 + " is BEFORE " + date2);
}
You will see the following output if you execute the above code:
2018-07-12 is AFTER 2016-08-25
Comparing two LocalTime
instances
A LocalTime
represents a time without date in ISO-8601 format (HH:mm:ss). Here is an example that shows how to compare two instances of LocalTime
in Java 8 and higher:
LocalTime time1 = LocalTime.parse("10:25:39");
LocalTime time2 = LocalTime.parse("14:20:00");
// compare `LocalTime` instances
System.out.println(time1 + " is BEFORE " + time2 + "? " + time1.isBefore(time2));
System.out.println(time1 + " is AFTER " + time2 + "? " + time1.isAfter(time2));
The above code will output the following:
10:25:39 is BEFORE 14:20? true
10:25:39 is AFTER 14:20? false
You can also compare two instances of LocalTime
by using compareTo()
method just like LocalDate
:
LocalTime time1 = LocalTime.of(22, 45, 12);
LocalTime time2 = LocalTime.of(8, 25, 25);
// compare times
int diff = time1.compareTo(time2);
if (diff == 0) {
System.out.println(time1 + " is EQUAL to " + time2);
} else if (diff > 0) {
System.out.println(time1 + " is AFTER " + time2);
} else {
System.out.println(time1 + " is BEFORE " + time2);
}
Here is what the output looks like now:
22:45:12 is AFTER 08:25:25
Comparing two LocalDateTime
instances
The LocalDateTime
class represents both date and time together without timezone in ISO-8601 format. To compare two instances of LocalDateTime
, you can use the same isAfter()
, isBefore()
, and isEqual()
methods as shown below:
LocalDateTime dt1 = LocalDateTime.parse("2016-04-12T12:55:15");
LocalDateTime dt2 = LocalDateTime.parse("2017-07-15T05:05:51");
// compare `LocalDateTime` instances
System.out.println(dt1 + " is BEFORE " + dt2 + ": " + dt1.isBefore(dt2));
System.out.println(dt1 + " is AFTER " + dt2 + ": " + dt1.isAfter(dt2));
System.out.println(dt1 + " is EQUAL to " + dt2 + ": " + dt1.isEqual(dt2));
The above code prints the following on the console:
2016-04-12T12:55:15 is BEFORE 2017-07-15T05:05:51: true
2016-04-12T12:55:15 is AFTER 2017-07-15T05:05:51: false
2016-04-12T12:55:15 is EQUAL to 2017-07-15T05:05:51: false
To use the compareTo()
method to compare two instances of LocalDateTime
, you can do the following:
LocalDateTime dt1 = LocalDateTime.of(1989, Month.AUGUST, 2, 10, 0);
LocalDateTime dt2 = LocalDateTime.of(1992, Month.MARCH, 23, 17, 33);
// compare dates and times
int diff = dt1.compareTo(dt2);
if (diff == 0) {
System.out.println(dt1 + " is EQUAL to " + dt2);
} else if (diff > 0) {
System.out.println(dt1 + " is AFTER " + dt2);
} else {
System.out.println(dt1 + " is BEFORE " + dt2);
}
If you execute the above code snippet, you should see the following output:
1989-08-02T10:00 is BEFORE 1992-03-23T17:33
Comparing two ZonedDateTime
instances
A ZonedDateTime
represents a date and time with a timezone in ISO-8601 format (e.g 2015-05-15T10:15:30+01:00[Europe/Paris]). It is basically used to deal with timezone-specific dates and times.
The following example demonstrates how you can compare two instances of ZonedDateTime
in Java:
ZonedDateTime dt1 = ZonedDateTime.parse("2017-05-15T10:15:30+01:00[Asia/Karachi]");
ZonedDateTime dt2 = ZonedDateTime.parse("2015-05-15T10:15:30+01:00[Europe/Paris]");
// compare `ZonedDateTime` instances
System.out.println(dt1 + " is BEFORE " + dt2 + ": " + dt1.isBefore(dt2));
System.out.println(dt1 + " is AFTER " + dt2 + ": " + dt1.isAfter(dt2));
System.out.println(dt1 + " is EQUAL to " + dt2 + ": " + dt1.isEqual(dt2));
You should see the following output for the above code snippet:
2017-05-15T10:15:30+05:00[Asia/Karachi] is BEFORE 2015-05-15T10:15:30+02:00[Europe/Paris]: false
2017-05-15T10:15:30+05:00[Asia/Karachi] is AFTER 2015-05-15T10:15:30+02:00[Europe/Paris]: true
2017-05-15T10:15:30+05:00[Asia/Karachi] is EQUAL to 2015-05-15T10:15:30+02:00[Europe/Paris]: false
ZonedDateTime
also provides the compareTo()
method to perform the comparison between two of its instances:
ZonedDateTime dt1 = ZonedDateTime.of(2015, 7, 12, 10, 0, 0, 0, ZoneId.of("Europe/Paris"));
ZonedDateTime dt2 = ZonedDateTime.of(2019, 12, 5, 17, 0, 0, 0, ZoneId.of("America/New_York"));
// compare zoned dates and times
int diff = dt1.compareTo(dt2);
if (diff == 0) {
System.out.println(dt1 + " is EQUAL to " + dt2);
} else if (diff > 0) {
System.out.println(dt1 + " is AFTER " + dt2);
} else {
System.out.println(dt1 + " is BEFORE " + dt2);
}
You should see the following output:
2015-07-12T10:00+02:00[Europe/Paris] is BEFORE 2019-12-05T17:00-05:00[America/New_York]
Comparing two OffsetDateTime
instances
An instance of OffsetDateTime
represents a date and time with an offset from UTC/Greenwich in the ISO-8601 format (e.g. 2018-06-30T23:15:30+03:30).
Let us look at the below example that shows how you can compare two instances of OffsetDateTime
:
OffsetDateTime dt1 = OffsetDateTime.parse("2018-06-30T23:15:30+03:30");
OffsetDateTime dt2 = OffsetDateTime.parse("2011-11-25T23:15:30-05:00");
// compare `OffsetDateTime` instances
System.out.println(dt1 + " is BEFORE " + dt2 + ": " + dt1.isBefore(dt2));
System.out.println(dt1 + " is AFTER " + dt2 + ": " + dt1.isAfter(dt2));
System.out.println(dt1 + " is EQUAL to " + dt2 + ": " + dt1.isEqual(dt2));
Here is the output of the above code snippet:
2018-06-30T23:15:30+03:30 is BEFORE 2011-11-25T23:15:30-05:00: false
2018-06-30T23:15:30+03:30 is AFTER 2011-11-25T23:15:30-05:00: true
2018-06-30T23:15:30+03:30 is EQUAL to 2011-11-25T23:15:30-05:00: false
Similarly you can use compareTo()
to compare two instances of OffsetDateTime
as shown below:
OffsetDateTime dt1 = OffsetDateTime.of(2011, 8, 25, 15, 40, 0, 0, ZoneOffset.of("+03:00"));
OffsetDateTime dt2 = OffsetDateTime.of(2017, 3, 15, 5, 15, 45, 0, ZoneOffset.of("-05:00"));
// compare offset dates and times
int diff = dt1.compareTo(dt2);
if (diff == 0) {
System.out.println(dt1 + " is EQUAL to " + dt2);
} else if (diff > 0) {
System.out.println(dt1 + " is AFTER " + dt2);
} else {
System.out.println(dt1 + " is BEFORE " + dt2);
}
The above code will output the following:
2011-08-25T15:40+03:00 is BEFORE 2017-03-15T05:15:45-05:00
Comparing two Instant
instances
An Instant
represents a specific moment on the timeline in UTC. Here is how you can compare two instances of Instant
:
Instant instant1 = Instant.parse("2019-10-20T12:55:30.00Z");
Instant instant2 = Instant.parse("2017-01-25T23:15:30.55Z");
// compare `Instant` instances
System.out.println(instant1 + " is BEFORE " + instant2 + ": " + instant1.isBefore(instant2));
System.out.println(instant1 + " is AFTER " + instant2 + ": " + instant1.isAfter(instant2));
Here is the output of the above code snippet:
2019-10-20T12:55:30Z is BEFORE 2017-01-25T23:15:30.550Z: false
2019-10-20T12:55:30Z is AFTER 2017-01-25T23:15:30.550Z: true
Two instances of Instant
can also be compared by using compareTo()
as shown below:
Instant instant1 = Instant.parse("2019-10-20T12:55:30.00Z");
Instant instant2 = Instant.parse("2017-01-25T23:15:30.55Z");
// compare offset dates and times
int diff = instant1.compareTo(instant2);
if (diff == 0) {
System.out.println(instant1 + " is EQUAL to " + instant2);
} else if (diff > 0) {
System.out.println(instant1 + " is AFTER " + instant2);
} else {
System.out.println(instant1 + " is BEFORE " + instant2);
}
Here is the output when we use compareTo()
to compare Instant
objects:
2019-10-20T12:55:30Z is AFTER 2017-01-25T23:15:30.550Z
Look at Introduction to Java 8 Date and Time API tutorial for more new date and time API examples.
Legacy Date
and Calendar
API
Before Java 8, java.util.Date
and java.util.Calendar
classes were used to manipulate dates and times. These legacy classes have many shortcomings fixed with the release of the new date and time API. But these classes are still actively used in old projects. So it is worth knowing how to compare two instances of Date
or Calendar
.
Both Date
and Calendar
classes provide the following method for date comparison.
Comparing two dates usingcompareTo()
method
The compareTo()
method compares two dates for ordering. It returns 0 if both dates are equal. Otherwise, a positive value if this date is greater than the specified date and a negative if less.
The following example shows how you can compare two instances of Date
by using the compareTo()
method:
try {
// specify date pattern
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
// parse string to date
Date date1 = format.parse("2019-10-20");
Date date2 = format.parse("2017-01-25");
// compare dates
int diff = date1.compareTo(date2);
if (diff == 0) {
System.out.println(date1 + " is EQUAL to " + date2);
} else if (diff > 0) {
System.out.println(date1 + " is AFTER " + date2);
} else {
System.out.println(date1 + " is BEFORE " + date2);
}
} catch (ParseException ex) {
ex.printStackTrace();
}
The above code will output something like the following:
Sun Oct 20 00:00:00 PKT 2019 is AFTER Wed Jan 25 00:00:00 PKT 2017
Similarly, you can also compare two instances of Calendar
using the compareTo()
method as shown below:
try {
// specify date pattern
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
// init instances of `Calcendar`
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
// parse string to date and set calendar date
cal1.setTime(format.parse("2019-10-20"));
cal2.setTime(format.parse("2017-01-25"));
// compare `Calendar` instances
int diff = cal1.compareTo(cal2);
if (diff == 0) {
System.out.println(cal1 + " is EQUAL to " + cal2);
} else if (diff > 0) {
System.out.println(cal1 + " is AFTER " + cal2);
} else {
System.out.println(cal1 + " is BEFORE " + cal2);
}
} catch (ParseException ex) {
ex.printStackTrace();
}
before()
, after()
& equal()
methods
A more user-friendly way to compare two instances of old date and time API is using the before()
, after()
, and eqauls()
methods. These methods return true
if this instance is before, after, or equal to the specified instance.
Here is an example that uses the above methods to compare two instances of Date
:
try {
// specify date pattern
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
// parse string to date
Date date1 = format.parse("2017-12-25");
Date date2 = format.parse("2016-08-05");
//// compare `Date` instances
System.out.println(date1 + " is BEFORE " + date2 + ": " + date1.before(date2));
System.out.println(date1 + " is AFTER " + date2 + ": " + date1.after(date2));
System.out.println(date1 + " is EQUAL to " + date2 + ": " + date1.equals(date2));
} catch (ParseException ex) {
ex.printStackTrace();
}
If you execute the above code snippet, you'd see the following output:
Mon Dec 25 00:00:00 PKT 2017 is BEFORE Fri Aug 05 00:00:00 PKT 2016: false
Mon Dec 25 00:00:00 PKT 2017 is AFTER Fri Aug 05 00:00:00 PKT 2016: true
Mon Dec 25 00:00:00 PKT 2017 is EQUAL to Fri Aug 05 00:00:00 PKT 2016: false
Finally, in the end, let us look at an example that demonstrates how to compare two instances of Calendar
API:
try {
// specify date pattern
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
// init instances of `Calcendar`
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
// parse string to date and set calendar date
cal1.setTime(format.parse("2010-08-20"));
cal2.setTime(format.parse("2019-01-25"));
//// compare `Calendar` instances
System.out.println(cal1 + " is BEFORE " + cal2 + ": " + cal1.before(cal2));
System.out.println(cal1 + " is AFTER " + cal2 + ": " + cal1.after(cal2));
System.out.println(cal1 + " is EQUAL to " + cal2 + ": " + cal1.equals(cal2));
} catch (ParseException ex) {
ex.printStackTrace();
}
Summary
In this article, we covered multiple ways to compare dates originating from different APIs in Java. We started with Java 8 new date and time API and discussed how to compare dates with or without time and timezones. In the end, we also looked at comparing dates using the legacy Date
and Calendar
API.
The new date and time API introduced in Java 8 provides a broad range of classes that have simplified working in dates and times. These classes are thread-safe, easier to use, and offer utility methods for performing various operations.
Read next: How to get current date and time in Java
✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.