Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Date Comparison

 
Alan Morgan
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

What i want to be able to do is take a given date and see if it is within 48hours (in to the future) of the current date.

The date I have only has accuracy to the day.

The only thing I've tried so far is to create both as GregorianCalendar instances and call getTimeInMillis() on them and make sure difference is less than 2 days worth of milliseconds.

Not a nice solution...anyone any better ideas ?
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to be nitpicky (but it might help you to see the issue from another angle), if your resolution is in *days*, determining the number of *hours* (or smaller time unit) provides more information than what is useful.

For instance, if it's 8:00 am, should 9:00 am the day after tomorrow evaluate to true or false (i.e., 49 hours)?
[ June 22, 2005: Message edited by: Steve Morrow ]
 
Alan Morgan
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fair point,

So what I want to check is if they are within 2 days of each other.

I could compare year, then month and then day.
But this starts getting hairy with the 28(29), 30, 31 day months possibly ?

Or am I missing your point and there's an easier way to do this ?

And what don't I like about it....well I guess its the fact that I want to address the issue in terms of hours ( or as you correctly point out days) and I am being reduced to working with miliseconds.
[ June 22, 2005: Message edited by: Alan Morgan ]
 
Manuel Moons
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about something like this?



I think it should do the trick.
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about something like this?
...
final long TWODAYS = 1000 * 60 *60 *24 *2;
...

I think it should do the trick.
See my previous point about resolution. He's only working with units of days. If it's 8:00 today, your example will return false for 9:00 on the day after tomorrow.
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's what I came up with off the cuff - I'm open to suggestions for improvement...
 
Jeffrey Spaulding
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, beautiful solutions.

But there is one advantage to getTimeInMillis.

Its only one line of code.

Could there be a more beautiful thing?

In fact getTimeInMillis was the first thing that came to my mind when
i read your question.

Does that make me a bad programmer? No way

Right on,

J.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic