Skip to content
Related Articles

Related Articles

Compare Dates in Java
  • Last Updated : 30 Oct, 2018

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

  1. Using Date.compareTo():

    Steps involved:

    1. Create an object for SimpleDateFormat class initialising it with the format yyyy-mm-dd.
    2. Initialize the date variables using the above objects.
    3. Use compareTo() function of the date class for the comparisons of dates
    4. Print the result.

    Program:




    // Java program to compare dates in Java
      
    import java.util.Date;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
      
    public class TestDate {
        public static void main(String args[])
            throws ParseException
        {
      
            // Create SimpleDateFormat object
            SimpleDateFormat
                sdfo
                = new SimpleDateFormat("yyyy-MM-dd");
      
            // Get the two dates to be compared
            Date d1 = sdfo.parse("2018-03-31");
            Date d2 = sdfo.parse("2012-03-31");
      
            // Print the dates
            System.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 d2
                System.out.println("Date1 is after Date2");
            }
      
            else if (d1.compareTo(d2) < 0) {
      
                // When Date d1 < Date d2
                System.out.println("Date1 is before Date2");
            }
      
            else if (d1.compareTo(d2) == 0) {
      
                // When Date d1 = Date d2
                System.out.println("Date1 is equal to Date2");
            }
        }
    }
    Output:



    Date1 : 2018-03-31
    Date2 : 2012-03-31
    Date1 is after Date2
    
  2. Using Date.before(), Date.after() and Date.equals().

    This method is simpler than the first one.

    Steps involved:

    1. Create an object for SimpleDateFormat class initialising it with the format yyyy-mm-dd.
    2. Initialize the date variables using the above objects.
    3. Use after() and before functions of the date class for the comparisons of dates
    4. Print the result.

    Program:




    // Java program to compare dates in Java
      
    import java.util.Date;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
      
    public class TestDate {
        public static void main(String args[])
            throws ParseException
        {
      
            // Create SimpleDateFormat object
            SimpleDateFormat
                sdfo
                = new SimpleDateFormat("yyyy-MM-dd");
      
            // Get the two dates to be compared
            Date d1 = sdfo.parse("2018-03-31");
            Date d2 = sdfo.parse("2012-03-31");
      
            // Print the dates
            System.out.println("Date1 : " + sdfo.format(d1));
            System.out.println("Date2 : " + sdfo.format(d2));
      
            // Compare the dates
            if (d1.after(d2)) {
      
                // When Date d1 > Date d2
                System.out.println("Date1 is after Date2");
            }
      
            else if (d1.before(d2)) {
      
                // When Date d1 < Date d2
                System.out.println("Date1 is before Date2");
            }
      
            else if (d1.equals(d2)) {
      
                // When Date d1 = Date d2
                System.out.println("Date1 is equal to Date2");
            }
        }
    }
    Output:
    Date1 : 2018-03-31
    Date2 : 2012-03-31
    Date1 is after Date2
    
  3. Using Calender.before(), Calender.after() and Calender.equals().

    Steps involved:

    1. Create an object for SimpleDateFormat class initialising it with the format yyyy-mm-dd.
    2. Initialize the date variables using the above objects.
    3. Initialize the Calendar class objects using the getinstance() functions.
    4. Using the setTime() function of calendar class assign the values to the calendar objects.
    5. Use after() and before functions of the Calender class for the comparisons of dates
    6. Print the result.

    Program:




    // Java program to compare dates in Java
      
    import java.util.Date;
    import java.util.Calendar;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
      
    public class TestDate {
        public static void main(String args[])
            throws ParseException
        {
      
            // Create SimpleDateFormat object
            SimpleDateFormat
                sdfo
                = new SimpleDateFormat("yyyy-MM-dd");
      
            // Get the two dates to be compared
            Date d1 = sdfo.parse("2018-03-31");
            Date d2 = sdfo.parse("2012-03-31");
      
            // Print the dates
            System.out.println("Date1 : " + sdfo.format(d1));
            System.out.println("Date2 : " + sdfo.format(d2));
      
            // Creating Calendar objects
            Calendar calen1 = Calendar.getInstance();
            Calendar calen2 = Calendar.getInstance();
            calen1.setTime(d1);
            calen2.setTime(d2);
      
            // Compare the dates
            if (calen1.after(calen2)) {
      
                // When Date d1 > Date d2
                System.out.println("Date1 is after Date2");
            }
      
            else if (calen1.before(calen2)) {
      
                // When Date d1 < Date d2
                System.out.println("Date1 is before Date2");
            }
      
            else if (calen1.equals(calen2)) {
      
                // When Date d1 = Date d2
                System.out.println("Date1 is equal to Date2");
            }
        }
    }
    Output:
    Date1 : 2018-03-31
    Date2 : 2012-03-31
    Date1 is after Date2
    
  4. 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:

    1. Create objects of LocalDate class.
    2. Use the isAfter(), isBefore() and isEqual() functions of date class to compare the dates.
    3. Print the result.

    Program:




    // Java program to compare Dates
      
    import java.time.LocalDate;
      
    public class Compare_dates {
        public static void main(String[] args)
        {
      
            // Create LocalDate objects with dates
            LocalDate d1 = LocalDate.of(2016, 12, 15);
            LocalDate d2 = LocalDate.of(2017, 01, 15);
      
            // Print the Dates
            System.out.println("Date1: " + d1);
            System.out.println("Date2: " + d2);
      
            // Compare the dates
            if (d1.isAfter(d2)) {
      
                // When Date d1 > Date d2
                System.out.println("Date1 is after Date2");
            }
      
            else if (d1.isBefore(d2)) {
      
                // When Date d1 < Date d2
                System.out.println("Date1 is before Date2");
            }
      
            else if (d1.isEqual(d2)) {
      
                // When Date d1 = Date d2
                System.out.println("Date1 is equal to Date2");
            }
        }
    }
    Output:
    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 Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :