Compare Two String Dates in Java with Examples

During development we are required to compare dates given in string format for some scenarios. For example date string retrieved from the database and required to compare with today’s date or other given string date.

In such cases it requires parsing date strings before further comparison manipulations. A java class SimpleDateFormat used to parse the date from a string. SimpleDateFormat is also used to format dates based on different patterns.

Let’s explore below example of parsing date string

String dateString = "2021-05-15";
        
        try {

            SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd");

            Date d = dt.parse(dateString);

            System.out.println("The date is: " + dt.format(d));

        } catch (ParseException e) {
            e.printStackTrace();
        }

Explanation: Let's suppose the given date string is "2021-05-15" as described in above examples. Now we need to convert the date string into a Date object by parsing the string.

SimpleDateFormat used to parse date strings with pattern yyyy-MM-dd here. dt.parse(dateString) return date object once parsed. We should handle this parsing exception using try catch block because parse throws ParseException.

After parsing the date string we can further use the date object for comparing purposes.

Let’s review in the following ways.

1. How to compare two string dates using the compareTo method?

java.util.Date.compareTo is used to compare instance date with specified date.

Syntax: int result = dateObj1.compareTo(dateObj2);

Returns

  • 0 if both the instance date and passed date are equal.
  • Less than 0 if dateObj1 is less than dateObj2
  • Greater than 0 if dateObj1 is greater than dateObj2

Refer below program for demonstration to Compare Two String Dates in Java

package javaapplication;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class JavaApplication {

    public static void main(String args[]) {

        String fromDateString = "2021-06-15";
        
        String toDateString = "2021-07-15";

        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

            Date dateFrom = dateFormat.parse(fromDateString);
            
            Date dateTo = dateFormat.parse(toDateString);
            
         
            if (dateFrom.compareTo(dateTo) > 0) {
                System.out.println("From Date is greater than To Date");
                
            } else if (dateFrom.compareTo(dateTo) < 0) {
                
               System.out.println("From Date is less than To Date");
               
            } else if (dateFrom.compareTo(dateTo) == 0) {
                
                  System.out.println("From Date is Equal to To Date");
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }

    }

}
 

Output: From Date is less than To Date

2. How to compare two string dates with different date formats in Java?

In the previous example we learned about date comparison with the same format. But there can be cases where we received date strings in different date formats such as dd/MM/yyyy, MM/dd/yyyy etc.

In such a case first we need to parse given string dates based on their pattern. Let’s understand with following example code

package javaapplication;

import java.text.*;
import java.util.Date;
import java.text.ParseException;

public class date {

    public static void main(String[] args) {

        String startDate = "21-08-2027";
        String endDate = "21/08/2021";

        try {

            Date dateStart = new SimpleDateFormat("dd-MM-yyyy").parse(startDate);

            Date dateEnd = new SimpleDateFormat("dd/MM/yyyy").parse(endDate);

            if (dateStart.after(dateEnd)) {
                System.out.println("Start Date is greater than End Date");

            } else if (dateStart.before(dateEnd)) {

                System.out.println("Start Date is less than End Date");

            } else {

                System.out.println("Start Date is Equal to End Date");
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }

    }
}
 

Output: Start Date is greater than End Date

Explanation: As we can see, the given start Date is of "dd-MM-yyyy" format where the end date is of "dd/MM/yyyy" format. Therefore it required parse date string by their pattern such as

Date dateStart = new SimpleDateFormat("dd-MM-yyyy").parse(startDate);

Date dateEnd = new SimpleDateFormat("dd/MM/yyyy").parse(endDate);

Here method after & before method to check whether date instance is greater or less than specified date respectively.

3. Compare two string Date using LocalDate class methods

With the introduction of new api java.time it helps to solve many problems with old date api.

java.util is ok for basic tasks but for complex tasks it is better to use java.time packages class methods as applicable.

LocalDate is a class of java.time package. Here we are going to learn about using LocalDate compare methods for two date strings.

So let’s begin by writing the above example code for comparing dates using LocalDate class methods.

package javaapplication;

import java.time.*;
import java.text.ParseException;

public class date {

    public static void main(String[] args) throws ParseException {

        String startDate = "2021-05-12";
        
        String endDate =  "2021-04-12";

        LocalDate dateStart = LocalDate.parse(startDate);

        LocalDate dateEnd = LocalDate.parse(endDate);

        if (dateStart.isAfter(dateEnd)) {
            System.out.println("Start Date is greater than End Date");

        } else if (dateStart.isBefore(dateEnd)) {

            System.out.println("Start Date is less than End Date");

        } else {

            System.out.println("Start Date is Equal to End Date");
        }

    }
}
 

Output: Start Date is greater than End Date

Explanation: Date string is parsed using LocalDate.parse method. Here compare method used as

  • isAfter - Return true if LocalDate instance is greater than passed date.
  • isBefore - Return true if LocalDate instance is less than passed date.