2011-10-16 9 views
11

Powiel możliwe:
How to compare two Dates without the time portion?Porównaj datę bez czasu

Jak porównać datę bez czasu w Java?

Date currentDate = new Date();// get current date   
Date eventDate = tempAppointments.get(i).mStartDate; 
int dateMargin = currentDate.compareTo(eventDate); 

ten kod porównuje godzinę i datę!

+2

to powtórzyć z [stary post] [1] [1]: http://stackoverflow.com/questions/1439779/how-to-compare-two-dates-without-the -czasowa porcja – Saurabh

+0

@saury: dzięki za link! W jakiś sposób wiedziałem, że Joda Time będzie to gładko obsługiwane. –

+0

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% –

Odpowiedz

34

Spróbuj porównać do zmieniających termin 00:00:00 swoim czasie (jak tej funkcji zrobić):

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

Można napisać metodę Date withoutTime(Date), która zwraca kopię daty, w której wszystkie pola czasowe (godzina, minuta, sekunda, milli) są ustawione na zero. Następnie możesz je porównać.

Lub możesz przełączyć się na Czas Joda, jeśli to możliwe. Ta biblioteka ma już typ danych DateMidnight, którego właśnie szukasz.

1

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ę.

Powiązane problemy