12

Possible Duplicate:
How to compare two Dates without the time portion?

How to compare date without time in java ?

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

this code compares time and date !

Community
  • 1
  • 1
Adham
  • 63,550
  • 98
  • 229
  • 344
  • 2
    This is again repeat of [old post][1] [1]: http://stackoverflow.com/questions/1439779/how-to-compare-two-dates-without-the-time-portion – Saurabh Oct 16 '11 at 12:02
  • @saury: thanks for the link! Somehow I knew that Joda Time would have this handled in a smooth fashion. – Hovercraft Full Of Eels Oct 16 '11 at 12:25
  • This is an interesting topic since I have a similar requirement, but had to do large volumes of date compares, and without using a third party library if possible. Below are basic benchmarks of comparing the same two dates using various methods: * Compare using deprecated getYear, getMonth, and getDate: time=125ms for 500000 compares * Compare using SimpleDateFormat and Integer.parseInt: time=719ms for 500000 compares +475% * Compare using Calendar.get() methods: time=2297ms for 500000 compares +1738% * Compare by setting time to midnight: time=2516ms for 500000 compares +1913% – Ali Cheaito Aug 26 '13 at 18:33

4 Answers4

46

Try compare dates changing to 00:00:00 its time (as this function do):

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));
yoprogramo
  • 1,306
  • 9
  • 14
4

You can write a method Date withoutTime(Date) that returns a copy of the date in which all time fields (hour, minute, second, milli, time zone) are set to zero. Then you can compare these.

Or you can switch to Joda Time if possible. That library already has the data type DateMidnight, which is what you are looking for.

Roland Illig
  • 40,703
  • 10
  • 88
  • 121
2

Write your own method which does not take the time into account:

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 ;
   }
}

Note that methods getYear(), getMonth() and getDate() have been deprecated. You should go through the Calendar class and perform the same method.

Pierre
  • 2,858
  • 22
  • 21
1

Use truncate function: http://commons.apache.org/lang/api-2.5/org/apache/commons/lang/time/DateUtils.html

dbf
  • 6,399
  • 2
  • 38
  • 65
  • 1
    Or use truncatedCompareTo() http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/time/DateUtils.html#truncatedCompareTo%28java.util.Calendar,%20java.util.Calendar,%20int%29 – shonky linux user Jul 21 '13 at 02:14