Java, Software Development

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.

Convert String value to java.util.Date

These codes generate the following output.

Convert String value to java.sql.Date

The codes output the following.

Convert String value to java.time.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.

The codes output Date1 and Date2 are equal!. What about when date1 is before date2?

The output is Date1 is before Date2!. What about where 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.

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.


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.


How to Compare Dates in Java That Are Of Different Type?

Let’s say we have the following dates.

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.

Loading

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like