Groovy - Dates & Times compareTo()



Compares two Dates for ordering.

Syntax

public int compareTo(Date anotherDate)

Parameters

anotherDate – the Date to be compared.

Return Value − The value 0 if the argument Date is equal to this Date; a value less than 0 if this Date is before the Date argument; and a value greater than 0 if this Date is after the Date argument.

Example

Following is an example of the usage of this method −

class Example { 
   static void main(String[] args) { 
      Date olddate = new Date("05/11/2015"); 
      Date newdate = new Date("05/11/2015"); 
      Date latestdate = new Date(); 
		
      System.out.println(olddate.compareTo(newdate)); 
      System.out.println(latestdate.compareTo(newdate)); 
   } 
}

When we run the above program, we will get the following result −

0 
1 
groovy_dates_times.htm
Advertisements