Compare two dates in Java
This post will discuss how to compare two dates in Java.
1. Using java.util.Date class
To compare two java.util.Date objects, we can use the compareTo() method. This method returns an integer value indicating the order of the two dates, by comparing the specified date with the current date based on their millisecond values. The return value is 0 if the dates are equal, negative if the current date is earlier than the specified date, and positive if the current date is later than the specified date. The following program demonstrates its usage:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; class Main { public static void main(String[] args) { String d1 = "10-20-2016"; String d2 = "10-12-2015"; String pattern = "MM-dd-yyyy"; SimpleDateFormat sdf = new SimpleDateFormat(pattern); try { Date first_date = sdf.parse(d1); Date second_date = sdf.parse(d2); int ret = first_date.compareTo(second_date); if (ret > 0) { System.out.printf("%s is after %s", d1, d2); } else if (ret < 0) { System.out.printf("%s is before %s", d1, d2); } else { System.out.print("Both dates are equal"); } } catch (ParseException e) { e.printStackTrace(); } } } |
Output:
10-20-2016 is after 10-12-2015
The java.util.Date class also offers three more methods: before(), after() and equals(), which returns a boolean value depending upon the comparison result. The Date.after() method checks whether the date is later than the specified date, the Date.before() method checks whether the date is before than the specified date, and the Date.equals() method compares whether the dates are the same. The following program demonstrates its usage:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; class Main { public static void main(String[] args) { String d1 = "10-20-2016"; String d2 = "10-12-2015"; String pattern = "MM-dd-yyyy"; SimpleDateFormat sdf = new SimpleDateFormat(pattern); try { Date first_date = sdf.parse(d1); Date second_date = sdf.parse(d2); if (first_date.after(second_date)) { System.out.printf("%s is after %s", d1, d2); } else if (first_date.before(second_date)) { System.out.printf("%s is before %s", d1, d2); } else if (first_date.equals(second_date)) { System.out.print("Both dates are equal"); } } catch (ParseException e) { e.printStackTrace(); } } } |
Output:
10-20-2016 is after 10-12-2015
2. Using java.time.LocalDate class
In Java 8, we can use the LocalDate class from the java.time package, which represents a date without time and timezone information. We can compare two dates using the compareTo(), which returns a negative, zero, and positive value indicating whether the current date is before, equal to, or after the specified date, respectively. Here is an example of using this method:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import java.time.LocalDate; import java.time.format.DateTimeFormatter; class Main { public static void main(String[] args) { String d1 = "10-20-2016"; String d2 = "10-12-2015"; String pattern = "MM-dd-yyyy"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern); LocalDate date1 = LocalDate.parse(d1, formatter); LocalDate date2 = LocalDate.parse(d2, formatter); if (date1.compareTo(date2) > 0) { System.out.printf("%s is after %s", d1, d2); } else if (date1.compareTo(date2) < 0) { System.out.printf("%s is before %s", d1, d2); } else { System.out.print("Both dates are equal"); } } } |
Output:
10-20-2016 is after 10-12-2015
We can also use java.time.LocalDateTime class that represents a date with time, without timezone information. The java.time.LocalDate and java.time.LocalDateTime class also offers three more methods: isAfter(), isBefore(), isEqual() method, which compares the date object with another date and return a boolean value depending on whether the date object is later than, earlier than, or equal to the other date, respectively. These methods compare the dates based on their chronological order. The following program demonstrates its usage:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import java.time.LocalDate; import java.time.format.DateTimeFormatter; class Main { public static void main(String[] args) { String d1 = "10-20-2016"; String d2 = "10-12-2015"; String pattern = "MM-dd-yyyy"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern); LocalDate date1 = LocalDate.parse(d1, formatter); LocalDate date2 = LocalDate.parse(d2, formatter); if (date1.isAfter(date2)) { System.out.printf("%s is after %s", d1, d2); } else if (date1.isBefore(date2)) { System.out.printf("%s is before %s", d1, d2); } else if (date1.isEqual(date2)) { System.out.print("Both dates are equal"); } } } |
Output:
10-20-2016 is after 10-12-2015
That’s all about comparing two dates in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)