Groovy - Dates & Times compareTo()

比较两个日期。


语法

public int compareTo(Date anotherDate)

参数

anotherDate – 要比较的日期。

返回值 − 如果参数 Date 等于 anotherDate,则值为 0; 如果此 Date 在 anotherDate 参数之前,则值小于 0; 如果此 Date 在 anotherDate 参数之后,则值大于 0


示例

以下是该方法的用法示例 −

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)); 
   } 
}

当我们运行上面的程序时,会得到下面的结果 −

0 
1 

❮ Groovy 日期与时间