aspose file tools*
The moose likes Java in General and the fly likes Date Comparison Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Date Comparison" Watch "Date Comparison" New topic
Author

Date Comparison

Alan Morgan
Ranch Hand

Joined: Apr 18, 2005
Posts: 113
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

Joined: May 22, 2003
Posts: 657

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

Joined: Apr 18, 2005
Posts: 113
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

Joined: Mar 05, 2002
Posts: 229
What about something like this?



I think it should do the trick.
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

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

Joined: May 22, 2003
Posts: 657

Here's what I came up with off the cuff - I'm open to suggestions for improvement...
Jeffrey Spaulding
Ranch Hand

Joined: Jan 15, 2004
Posts: 149
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Date Comparison