views:

378

answers:

3

Hi

I need to compare two dates in java. I am using the code like this:

Date questionDate = question.getStartDate();
Date today = new Date();

if(today.equals(questionDate)){
    System.out.println("Both are equals");
}

But this is not working. Results for both Dates are like this:

questionDate returns like this: 2010-06-30 00:31:40.0
today returns like this: Wed Jun 30 01:41:25 IST 2010

How to resolve this? Thanks in advance

+10  A: 

Date equality depends on the two dates being equal to the millisecond. If you are creating a new Date object using "new Date()", it will never be equal to a date created in the past. I would personally recommend using Joda Time's APIs for doing this since it simplifies working with dates. However, if you want to use the SDK alone:

if (removeTime(questionDate).equals(removeTime(today)) 
  ...

public Date removeTime(Date date) {    
    Calendar cal = Calendar.getInstance();  
    cal.setTime(date);  
    cal.set(Calendar.HOUR_OF_DAY, 0);  
    cal.set(Calendar.MINUTE, 0);  
    cal.set(Calendar.SECOND, 0);  
    cal.set(Calendar.MILLISECOND, 0);  
    return cal.getTime(); 
}
Eric Hauser
+1 for Joda Time, http://joda-time.sourceforge.net/
Freiheit
You may need to adjust for time zone as well.
Matthew Flynn
Timezones are yet another reason to use Joda Time =)
Eric Hauser
Thanks Eric.It is working fine. I have one question on this. Instead of writing method to remove the time, cant we use the Dateformat? please clarify
Gnaniyar Zubair
Sure, you could use string format and compare the strings and it would work fine. However, I believe it is more "correct" to use the date and calendar objects. For one reason, all strings are interned so you are just wasting memory when creating strings for the dates.
Eric Hauser
Thanks for your clarification Eric. Can you pls explain how we can achieve this using calender object..?
Gnaniyar Zubair
+1  A: 

I would use JodaTime for this. Here is an example - lets say you want to find the difference in days between 2 dates.

DateTime startDate = new DateTime(some_date); 
DateTime endDate = new DateTime(); //current date
Days diff = Days.daysBetween(startDate, endDate);
System.out.println(diff.getDays());

JodaTime can be downloaded from here.

CoolBeans
yes i need to get 1 week before from startDate..thanks
Gnaniyar Zubair
A: 

It's not clear to me what you want, but I'll mention that the Date class also has a compareTo method, which can be used to determine with one call if two Date objects are equal or (if they aren't equal) which occurs sooner. This allows you to do something like:

switch (today.compareTo(questionDate)) {
    case -1:  System.out.println("today is sooner than questionDate");  break;
    case 0:   System.out.println("today and questionDate are equal");  break;
    case 1:   System.out.println("today is later than questionDate");  break;
    default:  System.out.println("Invalid results from date comparison"); break;
}

It should be noted that the API docs don't guarantee the results to be -1, 0, and 1, so you may want to use if-elses rather than a switch in any production code. Also, if the second date is null, you'll get a NullPointerException, so wrapping your code in a try-catch may be useful.

GreenMatt