Compare Dates In Java That Are Of Different Date Types
As of Java 8, there are at least three different types of dates available for developers. As a result, the usage of these various types of dates sometimes causes problems, especially when we compare dates in Java. We perform dates comparison in Java using available methods in
java.util.Date,
java.time.LocalDate, and
java.util.Calendar classes.
Different Date Types
We have the following date types that we can use in our codes.
The historic java.util.Date
JDBC-specific java.sql.Date
The new Java 8 Date/Time API date – java.time.LocalDate
Typically, the dates come in as a String value and we convert them to either type of date.
The
java.util.Date class has methods to allow us to perform date comparison in Java.
Compare Using
Date.compareTo(Date)
This method compares two dates. When this method returns 0, the two dates are equals. If it returns less than 0,
this date object is before the argument object date. Otherwise, it’s after the argument object date.
Let’s test with equal dates.
Java
1
2
3
4
5
6
7
8
9
10
11
12
// January 14, 2020
Stringstr_date1="01/14/2020";
Date date1=newjava.util.Date(str_date1);
// January 14, 2020
Stringstr_date2="01/14/2020";
Date date2=newjava.util.Date(str_date2);
if(date1.compareTo(date2)==0)
{
System.out.println("Date1 and Date2 are equal!");
}
The codes output
Date1 andDate2 are equal!. What about when
date1 is before
date2?
Java
1
2
3
4
5
6
7
8
9
10
11
12
// January 14, 2020
Stringstr_date1="01/14/2020";
Date date1=newjava.util.Date(str_date1);
// January 15, 2020
Stringstr_date2="01/15/2020";
Date date2=newjava.util.Date(str_date2);
if(date1.compareTo(date2)<0)
{
System.out.println("Date1 is before Date2!");
}
The output is
Date1 isbefore Date2!. What about where
date1 is after
date2?
Java
1
2
3
4
5
6
7
8
9
10
11
12
// January 16, 2020
Stringstr_date1="01/16/2020";
Date date1=newjava.util.Date(str_date1);
// January 15, 2020
Stringstr_date2="01/15/2020";
Date date2=newjava.util.Date(str_date2);
if(date1.compareTo(date2)>0)
{
System.out.println("Date1 is after Date2!");
}
The output is
Date1 isafter Date2!.
Compare using
Date.before(Date),
Date.after(Date), and
Date.equals(Date)
These three methods are more intuitive compared to
Date.compareTo(Date). We can replace the
if statements’ expressions as follows.
Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
...
if(date1.equals(date2))
{
System.out.println("Date1 and Date2 are equal!");
}
...
if(date1.before(date2))
{
System.out.println("Date1 is before Date2!");
}
...
if(date1.after(date2)>0)
{
System.out.println("Date1 is after Date2!");
}
...
Using
Calendar.Before(Calendar),
Calendar.After(Calender), and
Calendar.equals(Calendar)
These methods work the same way as the previous three methods. But we need to create and use instances of
java.util.Calendar from the
java.util.Date objects.
Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
...
Calendar calDate1=Calendar.getInstance();
Calendar calDate2=Calendar.getInstance();
calDate1.setTime(date1);
calDate2.setTime(date2);
...
...
if(calDate1.equals(calDate2))
{
System.out.println("Date1 and Date2 are equal!");
}
...
if(calDate1.before(calDate2))
{
System.out.println("Date1 is before Date2!");
}
...
if(calDate1.after(calDate2)>0)
{
System.out.println("Date1 is after Date2!");
}
...
Compare Dates of Type
java.sql.Date
The class
java.sql.Date is a subclass of
java.util.Date and we use it primarily with the JDBC API. Therefore, we can use objects of either type interchangeably because they have all the methods to compare dates in Java.
Notice that we are using instances of
java.sql.Date class and we used the
Date.compareTo(Date) method! We can also use the other three methods –
Date.before(Date),
Date.after(Date), and
Date.equals(Date).
Compare Dates of Type
java.time.LocalDate
When Oracle released Java 8, it shipped a new API for date and time that includes the
java.time.LocalDate class. Fortunately, this class has its method for dates comparison in Java. Consider the following codes.
// QUESTION: How to compare localDate1 with javaUtilDate?
Before we can compare those dates, we need to convert them to the same type. We could either convert
localDate1 to
java.util.Date or
javaUtilDate to
java.time.LocalDate. Thereafter, we can use the methods available to date comparison.
Java
1
2
3
4
5
6
7
8
...
Date javaUtilDateFromLocalDate=Date.from(localDate1.atStartOfDay(ZoneId.systemDefault()).toInstant());