比較 Date 物件
日曆,日期和 LocalDate
Version < Java SE 8
before,after,compareTo 和 equals 方法
//Use of Calendar and Date objects
final Date today = new Date();
final Calendar calendar = Calendar.getInstance();
calendar.set(1990, Calendar.NOVEMBER, 1, 0, 0, 0);
Date birthdate = calendar.getTime();
final Calendar calendar2 = Calendar.getInstance();
calendar2.set(1990, Calendar.NOVEMBER, 1, 0, 0, 0);
Date samebirthdate = calendar2.getTime();
//Before example
System.out.printf("Is %1$tF before %2$tF? %3$b%n", today, birthdate, Boolean.valueOf(today.before(birthdate)));
System.out.printf("Is %1$tF before %1$tF? %3$b%n", today, today, Boolean.valueOf(today.before(today)));
System.out.printf("Is %2$tF before %1$tF? %3$b%n", today, birthdate, Boolean.valueOf(birthdate.before(today)));
//After example
System.out.printf("Is %1$tF after %2$tF? %3$b%n", today, birthdate, Boolean.valueOf(today.after(birthdate)));
System.out.printf("Is %1$tF after %1$tF? %3$b%n", today, birthdate, Boolean.valueOf(today.after(today)));
System.out.printf("Is %2$tF after %1$tF? %3$b%n", today, birthdate, Boolean.valueOf(birthdate.after(today)));
//Compare example
System.out.printf("Compare %1$tF to %2$tF: %3$d%n", today, birthdate, Integer.valueOf(today.compareTo(birthdate)));
System.out.printf("Compare %1$tF to %1$tF: %3$d%n", today, birthdate, Integer.valueOf(today.compareTo(today)));
System.out.printf("Compare %2$tF to %1$tF: %3$d%n", today, birthdate, Integer.valueOf(birthdate.compareTo(today)));
//Equal example
System.out.printf("Is %1$tF equal to %2$tF? %3$b%n", today, birthdate, Boolean.valueOf(today.equals(birthdate)));
System.out.printf("Is %1$tF equal to %2$tF? %3$b%n", birthdate, samebirthdate,
Boolean.valueOf(birthdate.equals(samebirthdate)));
System.out.printf(
"Because birthdate.getTime() -> %1$d is different from samebirthdate.getTime() -> %2$d, there are millisecondes!%n",
Long.valueOf(birthdate.getTime()), Long.valueOf(samebirthdate.getTime()));
//Clear ms from calendars
calendar.clear(Calendar.MILLISECOND);
calendar2.clear(Calendar.MILLISECOND);
birthdate = calendar.getTime();
samebirthdate = calendar2.getTime();
System.out.printf("Is %1$tF equal to %2$tF after clearing ms? %3$b%n", birthdate, samebirthdate,
Boolean.valueOf(birthdate.equals(samebirthdate)));
Version => Java SE 8
isBefore,isAfter,compareTo 和 equals 方法
//Use of LocalDate
final LocalDate now = LocalDate.now();
final LocalDate birthdate2 = LocalDate.of(2012, 6, 30);
final LocalDate birthdate3 = LocalDate.of(2012, 6, 30);
//Hours, minutes, second and nanoOfsecond can also be configured with an other class LocalDateTime
//LocalDateTime.of(year, month, dayOfMonth, hour, minute, second, nanoOfSecond);
//isBefore example
System.out.printf("Is %1$tF before %2$tF? %3$b%n", now, birthdate2, Boolean.valueOf(now.isBefore(birthdate2)));
System.out.printf("Is %1$tF before %1$tF? %3$b%n", now, birthdate2, Boolean.valueOf(now.isBefore(now)));
System.out.printf("Is %2$tF before %1$tF? %3$b%n", now, birthdate2, Boolean.valueOf(birthdate2.isBefore(now)));
//isAfter example
System.out.printf("Is %1$tF after %2$tF? %3$b%n", now, birthdate2, Boolean.valueOf(now.isAfter(birthdate2)));
System.out.printf("Is %1$tF after %1$tF? %3$b%n", now, birthdate2, Boolean.valueOf(now.isAfter(now)));
System.out.printf("Is %2$tF after %1$tF? %3$b%n", now, birthdate2, Boolean.valueOf(birthdate2.isAfter(now)));
//compareTo example
System.out.printf("Compare %1$tF to %2$tF %3$d%n", now, birthdate2, Integer.valueOf(now.compareTo(birthdate2)));
System.out.printf("Compare %1$tF to %1$tF %3$d%n", now, birthdate2, Integer.valueOf(now.compareTo(now)));
System.out.printf("Compare %2$tF to %1$tF %3$d%n", now, birthdate2, Integer.valueOf(birthdate2.compareTo(now)));
//equals example
System.out.printf("Is %1$tF equal to %2$tF? %3$b%n", now, birthdate2, Boolean.valueOf(now.equals(birthdate2)));
System.out.printf("Is %1$tF to %2$tF? %3$b%n", birthdate2, birthdate3, Boolean.valueOf(birthdate2.equals(birthdate3)));
//isEqual example
System.out.printf("Is %1$tF equal to %2$tF? %3$b%n", now, birthdate2, Boolean.valueOf(now.isEqual(birthdate2)));
System.out.printf("Is %1$tF to %2$tF? %3$b%n", birthdate2, birthdate3, Boolean.valueOf(birthdate2.isEqual(birthdate3)));
Java 8 之前的日期比較
在 Java 8 之前,可以使用 java.util.Calendar 和 java.util.Date 類來比較日期。Date 類提供了 4 種比較日期的方法:
after
,before
,compareTo
和 equals
方法比較每個日期的 getTime()
方法返回的值。
compareTo
方法返回正整數。
- 值大於 0:當 Date 在 Date 引數之後
- 值大於 0:當 Date 在 Date 引數之前時
- 值等於 0:當 Date 等於 Date 引數時
如示例所示,equals
結果可能會令人驚訝,因為如果未明確給出,則值(如毫秒)不會使用相同的值進行初始化。
自 Java 8 起
使用 Java 8,可以使用 java.time.LocalDate 來使用與 Date 一起使用的新 Object。LocalDate
實現 ChronoLocalDate ,它是可以插入 Chronology 或日曆系統的日期的抽象表示。
要具有日期時間精度,必須使用 Object java.time.LocalDateTime 。LocalDate
和 LocalDateTime
使用相同的方法名稱進行比較。
使用 LocalDate
比較日期與使用 ChronoLocalDate
不同,因為時間順序或日曆系統不考慮第一個。
因為大多數應用程式應該使用 LocalDate
,所以示例中不包含 ChronoLocalDate
。進一步閱讀這裡 。
大多數應用程式應將方法簽名,欄位和變數宣告為 LocalDate,而不是此[ChronoLocalDate]介面。
LocalDate
有 5 種方法來比較日期:
- isAfter(ChronoLocalDate 其他)
- isBefore(ChronoLocalDate 其他)
- isEqual(ChronoLocalDate 其他)
- compareTo(ChronoLocalDate 其他)
- equals(Object obj)
在 LocalDate
引數的情況下,isAfter
,isBefore
,isEqual
,equals
和 compareTo
現在使用這個方法:
int compareTo0(LocalDate otherDate) {
int cmp = (year - otherDate.year);
if (cmp == 0) {
cmp = (month - otherDate.month);
if (cmp == 0) {
cmp = (day - otherDate.day);
}
}
return cmp;
}
equals
方法檢查引數引用是否等於日期,而 isEqual
直接呼叫 compareTo0
。
如果是 ChronoLocalDate
的其他類例項,則使用 Epoch Day
比較日期。大紀元日計數是一個簡單的遞增計數天,其中第 0 天是 1970-01-01(ISO)
。