• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Tim Cooke
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
Saloon Keepers:
  • Piet Souris
Bartenders:

java.util.Date problem

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
author and iconoclast
Posts: 24208
47
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24208
47
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic