Napisz własną metodę, która nie bierze pod czas konto:
public static int compareDate(Date date1, Date date2) {
if (date1.getYear() == date2.getYear() &&
date1.getMonth() == date2.getMonth() &&
date1.getDate() == date2.getDate()) {
return 0 ;
}
else if (date1.getYear() < date1.getYear() ||
(date1.getYear() == date2.getYear() &&
date1.getMonth() < date2.getMonth()) ||
(date1.getYear() == date2.getYear() &&
date1.getMonth() == date2.getMonth() &&
date1.getDate() < date2.getDate()) {
return -1 ;
}
else {
return 1 ;
}
}
Należy zauważyć, że metody getYear()
, getMonth()
i getDate()
były przestarzałe. Powinieneś przejść przez klasę Calendar
i wykonać tę samą metodę.
to powtórzyć z [stary post] [1] [1]: http://stackoverflow.com/questions/1439779/how-to-compare-two-dates-without-the -czasowa porcja – Saurabh
@saury: dzięki za link! W jakiś sposób wiedziałem, że Joda Time będzie to gładko obsługiwane. –
To interesujący temat, ponieważ mam podobny wymóg, ale musiałem porównywać duże ilości danych i jeśli to możliwe, nie korzystałem z biblioteki stron trzecich. Poniżej znajdują się podstawowe testy porównujące te same dwie daty przy użyciu różnych metod: * Porównywanie za pomocą przestarzałych getYear, getMonth i getDate: time = 125ms dla 500000 porównuje * Porównaj przy użyciu SimpleDateFormat i Integer.parseInt: time = 719ms dla 500000 porównuje +475 % * Porównaj przy użyciu metod Calendar.get(): czas = 2297 ms dla 500000 porównuje + 1738% * Porównaj, ustawiając czas do północy: czas = 2516 ms dla 500000 porównań + 1913% –