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, the date string was retrieved from the database and required to be compared with today's date or another given string date.
In such cases it requires parsing date strings before further comparison manipulations. A Java class SimpleDateFormat is used to parse the date from a string. SimpleDateFormat is also used to format dates based on different patterns.
Let's explore the 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 a 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.
Scroll for More Useful Information and Relevant FAQs
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
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.
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.
Compare two Time Strings in java
Let’s understand with the following example:
package javaapplication;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class JavaApplication {
public static void main(String args[]) {
String startTimeString = "16:30:30";
String endTimeString = "14:30:30";
try {
SimpleDateFormat timeFormat = new SimpleDateFormat("hh:mm:ss");
Date timeFrom = timeFormat.parse(startTimeString);
Date timeTo = timeFormat.parse(endTimeString);
if (timeFrom.after(timeTo) ) {
System.out.println("Start Time > End Time");
} else if (timeFrom.before(timeTo)) {
System.out.println("Start Time < End Time");
} else {
System.out.println("Start Time = End Time");
}
} catch (ParseException e) {
e.printStackTrace();
}
}
}
Result:
Start Time > End Time
Code Explanation:
- Given Start time string "16:30:30" and End Time String as "14:30:30"
- The input time is in string format therefore it is required to format and parse the received time string.
-
Given time string is in
hh:mm:ss
time patterns. Therefore parsing input time string using hh:mm:ss pattern here. -
timeFormat.parse
return date parsed from a string time. -
Now it is ready to compare the date once the received parsed date using
after
&before
Date methods as per the above code.
Note. Here we have used SimpleDateFormat using patterns hh:mm:ss. You may modify the pattern as per the input time string.
For example: Can use pattern hh:mm:ss a
If given time string is like 09:30:30 am