Date compareTo() method in Java with examples

The compareTo() method of Java Date class compares two dates and sort them for order.

Syntax:

public int compareTo(Date anotherDate)

Parameters: The function accepts a single parameter anotherDate which specifies the date to be compared .

Return Value: The function gives three return values specified below:

  • It returns the value 0 if the argument Date is equal to this Date.
  • It returns a value less than 0 if this Date is before the Date argument.
  • It returns a value greater than 0 if this Date is after the Date argument.

Exception: The function throws a single exception that is NullPointerException if anotherDate is null.

Program below demonstrates the above mentioned function:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to demonstrate
// compareTo() function of Date class
  
import java.util.Date;
import java.util.Calendar;
public class GfG {
    // main method
    public static void main(String[] args)
    {
  
        // creating a Calendar object
        Calendar c = Calendar.getInstance();
  
        // set Month
        // MONTH starts with 0 i.e. ( 0 - Jan)
        c.set(Calendar.MONTH, 11);
  
        // set Date
        c.set(Calendar.DATE, 05);
  
        // set Year
        c.set(Calendar.YEAR, 1996);
  
        // creating a date object with specifed time.
        Date dateOne = c.getTime();
  
        System.out.println("Date 1: "
                           + dateOne);
  
        // creating a date of object
        // storing the current date
        Date currentDate = new Date();
  
        System.out.println("Date 2: "
                           + currentDate);
  
        // compares
        System.out.println("On Comparison: "
                           + currentDate
                                 .compareTo(dateOne));
    }
}

chevron_right


Output:

Date 1: Thu Dec 05 08:17:55 UTC 1996
Date 2: Wed Jan 02 08:17:55 UTC 2019
On Comparison: 1
filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to demonstrate
// compareTo() function of Date class
  
import java.util.Date;
  
public class GfG {
    // main method
    public static void main(String[] args)
    {
  
        // creating a date of object
        // stospecified datent date
        Date currentDate = new Date();
  
        System.out.println("Date 1: " + currentDate);
  
        // speciefiedDate is assigned to null.
        Date specifiedDate = null;
  
        System.out.println("Date 2: " + specifiedDate);
  
        System.out.println("Passing null as parameter: ");
        try {
            // throws NullPointerException
            System.out.println(currentDate
                                   .compareTo(specifiedDate));
        }
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}

chevron_right


Output:

Date 1: Wed Jan 02 08:18:02 UTC 2019
Date 2: null
Passing null as parameter: 
Exception: java.lang.NullPointerException


My Personal Notes arrow_drop_up

Contented with little yet wishing for more

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.