嘗試比較日期更改爲00:00:00的時間(這個功能做):
public static Date getZeroTimeDate(Date fecha) {
Date res = fecha;
Calendar calendar = Calendar.getInstance();
calendar.setTime(fecha);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
res = calendar.getTime();
return res;
}
Date currentDate = new Date();// get current date
Date eventDate = tempAppointments.get(i).mStartDate;
int dateMargin = getZeroTimeDate(currentDate).compareTo(getZeroTimeDate(eventDate));
這再次重複[老帖] [1] [1]的:http://stackoverflow.com/questions/1439779/how-to-compare-two-dates-without-the時間部分 – Saurabh
@saury:感謝您的鏈接!不知何故,我知道Joda Time會以平穩的方式處理這件事。 –
這是一個有趣的話題,因爲我有類似的要求,但必須做大量的日期比較,並且如果可能的話不需要使用第三方庫。以下是使用各種方法比較相同兩個日期的基本基準: *使用棄用的getYear,getMonth和getDate進行比較:500000時間= 125ms比較 *使用SimpleDateFormat和Integer.parseInt進行比較:time = 719ms for 500000比較+475 % *使用Calendar.get()方法進行比較:time = 2297ms for 500000比較+ 1738% *將時間設置爲午夜進行比較:時間= 2516ms for 500000比較+ 1913% –