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.
Convert String value to java.util.Date
1 2 3 4 5 | String str_date = "01/14/2020"; // java.util.Date(String) is deprecated Date date = new java.util.Date(str_date); System.out.println(date); |
These codes generate the following output.
1 | Tue Jan 14 00:00:00 SGT 2020 |
Convert String value to java.sql.Date
1 2 3 4 5 6 7 | String str_date = "01/14/2020"; // java.util.Date(String) is deprecated Date date = new java.util.Date(str_date); Date sqlDate = new java.sql.Date(date.getTime()); System.out.println(sqlDate); |
The codes output the following.
1 | 2020-01-14 |
Convert String value to java.time.LocalDate
1 2 3 4 | String str_date = "01/14/2020"; final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM/dd/yyyy"); LocalDate localDate = LocalDate.parse(str_date, dtf); System.out.println(localDate); |
The codes output 2020-01-14.
Compare Dates of Type java.util.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.
1 2 3 4 5 6 7 8 9 10 11 12 | // January 14, 2020 String str_date1 = "01/14/2020"; Date date1 = new java.util.Date(str_date1); // January 14, 2020 String str_date2 = "01/14/2020"; Date date2 = new java.util.Date(str_date2); if(date1.compareTo(date2) == 0) { System.out.println("Date1 and Date2 are equal!"); } |
The codes output Date1 and Date2 are equal!. What about when date1 is before date2?
1 2 3 4 5 6 7 8 9 10 11 12 | // January 14, 2020 String str_date1 = "01/14/2020"; Date date1 = new java.util.Date(str_date1); // January 15, 2020 String str_date2 = "01/15/2020"; Date date2 = new java.util.Date(str_date2); if(date1.compareTo(date2) < 0) { System.out.println("Date1 is before Date2!"); } |
The output is Date1 is before Date2!. What about where date1 is after date2?
1 2 3 4 5 6 7 8 9 10 11 12 | // January 16, 2020 String str_date1 = "01/16/2020"; Date date1 = new java.util.Date(str_date1); // January 15, 2020 String str_date2 = "01/15/2020"; Date date2 = new java.util.Date(str_date2); if(date1.compareTo(date2) > 0) { System.out.println("Date1 is after Date2!"); } |
The output is Date1 is after 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.
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.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | ... // January 14, 2020 String str_date1 = "01/14/2020"; Date date1 = new java.util.Date(str_date1); // January 14, 2020 String str_date2 = "01/14/2020"; Date date2 = new java.util.Date(str_date2); java.sql.Date sqlDate1 = new java.sql.Date(date1.getTime()); java.sql.Date sqlDate2 = new java.sql.Date(date2.getTime()); if(sqlDate1.compareTo(sqlDate2) == 0) { System.out.println("Date1 and Date2 are equal!"); } ... |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | ... final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM/dd/yyyy"); String str_date1 = "01/14/2020"; LocalDate localDate1 = LocalDate.parse(str_date1, dtf); String str_date2 = "01/17/2020"; LocalDate localDate2 = LocalDate.parse(str_date2, dtf); if(localDate1.isEqual(localDate2)) { System.out.println("LocalDate1 is equals to LocalDate2"); } if(localDate1.isBefore(localDate2)) { System.out.println("LocalDate1 is before LocalDate2"); } if(localDate1.isAfter(localDate2)) { System.out.println("LocalDate1 is after LocalDate2"); } ... |
How to Compare Dates in Java That Are Of Different Type?
Let’s say we have the following dates.
1 2 3 4 5 6 7 8 9 10 11 | ... final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM/dd/yyyy"); String str_date1 = "01/14/2020"; LocalDate localDate1 = LocalDate.parse(str_date1, dtf); String str_date2 = "01/14/2020"; // java.util.Date(String) is deprecated Date javaUtilDate = new Date(str_date2); // 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.
1 2 3 4 5 6 7 8 | ... Date javaUtilDateFromLocalDate = Date.from(localDate1.atStartOfDay(ZoneId.systemDefault()).toInstant()); if(javaUtilDate.before(javaUtilDateFromLocalDate)) { ... } ... |