The Date class represents a specific instant in time, with millisecond precision. The Date class of java.util package implements Serializable, Cloneable and Comparable interface. It provides constructors and methods to deal with date and time with java.
The following are the methods to compare dates in java
- Using Date.compareTo():
Steps involved:
- Create an object for SimpleDateFormat class initialising it with the format yyyy-mm-dd.
- Initialize the date variables using the above objects.
- Use compareTo() function of the date class for the comparisons of dates
- Print the result.
Program:
// Java program to compare dates in Javaimportjava.util.Date;importjava.text.ParseException;importjava.text.SimpleDateFormat;publicclassTestDate {publicstaticvoidmain(String args[])throwsParseException{// Create SimpleDateFormat objectSimpleDateFormatsdfo=newSimpleDateFormat("yyyy-MM-dd");// Get the two dates to be comparedDate d1 = sdfo.parse("2018-03-31");Date d2 = sdfo.parse("2012-03-31");// Print the datesSystem.out.println("Date1 : "+ sdfo.format(d1));System.out.println("Date2 : "+ sdfo.format(d2));// Compare the dates using compareTo()if(d1.compareTo(d2) >0) {// When Date d1 > Date d2System.out.println("Date1 is after Date2");}elseif(d1.compareTo(d2) <0) {// When Date d1 < Date d2System.out.println("Date1 is before Date2");}elseif(d1.compareTo(d2) ==0) {// When Date d1 = Date d2System.out.println("Date1 is equal to Date2");}}}chevron_rightfilter_noneOutput:
Date1 : 2018-03-31 Date2 : 2012-03-31 Date1 is after Date2
- Using Date.before(), Date.after() and Date.equals().
This method is simpler than the first one.
Steps involved:
- Create an object for SimpleDateFormat class initialising it with the format yyyy-mm-dd.
- Initialize the date variables using the above objects.
- Use after() and before functions of the date class for the comparisons of dates
- Print the result.
Program:
// Java program to compare dates in Javaimportjava.util.Date;importjava.text.ParseException;importjava.text.SimpleDateFormat;publicclassTestDate {publicstaticvoidmain(String args[])throwsParseException{// Create SimpleDateFormat objectSimpleDateFormatsdfo=newSimpleDateFormat("yyyy-MM-dd");// Get the two dates to be comparedDate d1 = sdfo.parse("2018-03-31");Date d2 = sdfo.parse("2012-03-31");// Print the datesSystem.out.println("Date1 : "+ sdfo.format(d1));System.out.println("Date2 : "+ sdfo.format(d2));// Compare the datesif(d1.after(d2)) {// When Date d1 > Date d2System.out.println("Date1 is after Date2");}elseif(d1.before(d2)) {// When Date d1 < Date d2System.out.println("Date1 is before Date2");}elseif(d1.equals(d2)) {// When Date d1 = Date d2System.out.println("Date1 is equal to Date2");}}}chevron_rightfilter_noneOutput:Date1 : 2018-03-31 Date2 : 2012-03-31 Date1 is after Date2
- Using Calender.before(), Calender.after() and Calender.equals().
Steps involved:
- Create an object for SimpleDateFormat class initialising it with the format yyyy-mm-dd.
- Initialize the date variables using the above objects.
- Initialize the Calendar class objects using the getinstance() functions.
- Using the setTime() function of calendar class assign the values to the calendar objects.
- Use after() and before functions of the Calender class for the comparisons of dates
- Print the result.
Program:
// Java program to compare dates in Javaimportjava.util.Date;importjava.util.Calendar;importjava.text.ParseException;importjava.text.SimpleDateFormat;publicclassTestDate {publicstaticvoidmain(String args[])throwsParseException{// Create SimpleDateFormat objectSimpleDateFormatsdfo=newSimpleDateFormat("yyyy-MM-dd");// Get the two dates to be comparedDate d1 = sdfo.parse("2018-03-31");Date d2 = sdfo.parse("2012-03-31");// Print the datesSystem.out.println("Date1 : "+ sdfo.format(d1));System.out.println("Date2 : "+ sdfo.format(d2));// Creating Calendar objectsCalendar calen1 = Calendar.getInstance();Calendar calen2 = Calendar.getInstance();calen1.setTime(d1);calen2.setTime(d2);// Compare the datesif(calen1.after(calen2)) {// When Date d1 > Date d2System.out.println("Date1 is after Date2");}elseif(calen1.before(calen2)) {// When Date d1 < Date d2System.out.println("Date1 is before Date2");}elseif(calen1.equals(calen2)) {// When Date d1 = Date d2System.out.println("Date1 is equal to Date2");}}}chevron_rightfilter_noneOutput:Date1 : 2018-03-31 Date2 : 2012-03-31 Date1 is after Date2
- Using Java 8 isBefore(), isAfter(), isEqual() and compareTo() methods: In Java 8, the isBefore(), isAfter(), isEqual() and compareTo() are used to compare LocalDate, LocalTime and LocalDateTime.
Steps involved:
- Create objects of LocalDate class.
- Use the isAfter(), isBefore() and isEqual() functions of date class to compare the dates.
- Print the result.
Program:
// Java program to compare Datesimportjava.time.LocalDate;publicclassCompare_dates {publicstaticvoidmain(String[] args){// Create LocalDate objects with datesLocalDate d1 = LocalDate.of(2016,12,15);LocalDate d2 = LocalDate.of(2017,01,15);// Print the DatesSystem.out.println("Date1: "+ d1);System.out.println("Date2: "+ d2);// Compare the datesif(d1.isAfter(d2)) {// When Date d1 > Date d2System.out.println("Date1 is after Date2");}elseif(d1.isBefore(d2)) {// When Date d1 < Date d2System.out.println("Date1 is before Date2");}elseif(d1.isEqual(d2)) {// When Date d1 = Date d2System.out.println("Date1 is equal to Date2");}}}chevron_rightfilter_noneOutput:Date1: 2016-12-15 Date2: 2017-01-15 Date1 is before Date2
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Return all dates between two dates in an array in PHP
- Compare two dates using JavaScript
- Find the duration of difference between two dates in Java
- How to compare two arrays in Java?
- How to Initialize and Compare Strings in Java?
- Compare two strings lexicographically in Java
- Java Integer compare() method
- Short compare() method in Java
- Compare two Strings in Java
- Boolean compare() method in Java with Examples
- Byte compare() method in Java with examples
- Float compare() Method in Java with Examples
- Double compare() Method in Java with Examples
- How compare() method works in Java
- Java | ==, equals(), compareTo(), equalsIgnoreCase() and compare()
- Java Guava | Booleans.compare() method with Examples
- Java Guava | Ints.compare() method with Examples
- Java Guava | compare() method of Short Class with Examples
- Java Guava | Floats.compare() method with Examples
- Java Guava | Longs.compare() method with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

