Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.util.Date problem

 
Ronnie Ho
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Results:
false
1127793600000
1127793600000

Both beginDate and selectedDate are of type java.util.Date. Can someone explain the result? According to the API:

two Date objects are equal if and only if the getTime method returns the same long value for both.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, where do the Date objects come from?
 
Ronnie Ho
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'd think. The one from the database is probably a java.sql.Date, which is a subclass of java.util.Date. Still, they should compare equal if getTime() reports the same number.
 
Bimal Patel
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can you try using compareTo(<Date object> . If the result is 0, the two dates are equal. I don't know why equal is not working . This may help you.
 
Stuart Ash
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are two possible alternatives to this:

a. Create calendar objects and compare the relevant fields of the calendar instances (day, month, year...).

b. Change the code to something like

int tolerableDifference = 24 * 60 * 60 * 1000; // milliseconds
return Math.abs(beginDate.getTime() - selectedDate.getTime()) < tolerableDifference;

Here, it should return true only if they represent the same date upto the day. (Caution: This algorithm might need some testing/debugging thow.)

Perhaps the first one is preferable.

Hope this helps.
 
Akshay Kiran
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know why your code is not working, but I can offer a workaround.

Just try
Date begindate2= new Date(beginDate.getTime());

and then

selectedDate.equals(begindate2);
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic