Groovy - 日期和時間 compareTo()



比較兩個日期的順序。

語法

public int compareTo(Date anotherDate)

引數

anotherDate – 要比較的日期。

返回值 - 如果引數 Date 等於此 Date,則值為0;如果此 Date 早於 Date 引數,則值為小於 0;如果此 Date 遲於 Date 引數,則值為大於 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_dates_times.htm
廣告