Java Date Time - Java Date.compareTo(Date anotherDate)








Syntax

Date.compareTo(Date anotherDate) has the following syntax.

public int compareTo(Date anotherDate)

Example

In the following code shows how to use Date.compareTo(Date anotherDate) method.

/*from   w  w  w  .  j  a  v a 2  s  . co m*/

import java.util.Date;

public class Main {

   public static void main(String[] args) {

      Date date = new Date();
      Date date2 = new Date();

      // make 3 comparisons with them
      int comparison = date.compareTo(date2);
      int comparison2 = date2.compareTo(date);
      int comparison3 = date.compareTo(date);

      // print the results
      System.out.println("Comparison Result:" + comparison);
      System.out.println("Comparison2 Result:" + comparison2);
      System.out.println("Comparison3 Result:" + comparison3);

   }
}

The code above generates the following result.