This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes compare dates Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "compare dates" Watch "compare dates" New topic

compare dates

Kristol Crawley

Joined: Sep 17, 2003
Posts: 7
I am completely lost with this thing! I have to compare 2 dates and figure out if they're within x amount of hours. I'm testing with 2. I have to set the timezone to the input timezone. I create 2 gregorian dates to whatever the input timezone is. Then I add the hours to the comparison date.
My problem is that it's printing the timezone as cst (what I'm testing with) but the time is calculating in est. Also, it looks like the requested date also gets added. They're 2 separate dates so I don't see how one value could be affecting the other.
Can anyone see what I'm doing wrong with this?
This is the output:
Initial Set. current: Thu Feb 05 20:55:15 EST 2004 req: Thu Feb 05 20:55:15 EST 2004
Before set current: Thu Feb 05 22:55:15 EST 2004
Requested after reset Thu Feb 05 19:00:15 EST 2004
Now the biggee, is it within 2 hours? 1
Interactive Session Ended
This is the code:
//Converts GMT to CST
String[] ids = TimeZone.getAvailableIDs(-6 * 60 * 60 * 1000);
SimpleTimeZone cst = new SimpleTimeZone(-6 * 60 * 60 * 1000, ids[0]);
//Calculate the day light savings time changes
cst.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
cst.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);

GregorianCalendar varCurrentCal = new GregorianCalendar(cst);
Calendar varReqCal = new GregorianCalendar(cst);
System.out.println("Initial Set. current: " + varCurrentCal.getTime() + " req: " + varReqCal.getTime());

//requested is military format, use HOUR_OF_DAY
//.add forces recalculation
varCurrentCal.add(Calendar.HOUR_OF_DAY, 2);
//System.out.println("Added 2 hours to current: " + varCurrentCal.getTime());

//requested date/time entered
int year = 2004;
int month = 02;
int day = 05;
int hour = 18;
int minute = 00;

System.out.println("Before set current: " + varCurrentCal.getTime());

varReqCal.set(year,(month -1),day,hour,minute);
System.out.println("Requested after reset " + varReqCal.getTime());
int delThis = varCurrentCal.getTime().compareTo(varReqCal.getTime());
System.out.println("Now the biggee, is it within 2 hours? " + delThis);
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

You are getting the EST because that is your time zone. Change the time zone on your computer to another time zone and you�ll see your program report the time as being in that time zone.
There are some idiosyncrasies with using time zones and calendars. Take a look at example 372 - Converting Times Between Time Zones at for some information and a code sample on the subject. Then take a look at example 376 - Comparing Dates.
Also take a look at the JavaWorld articles Calculating Java dates and Working in Java time : Learn the basics of calculating elapsed time in Java - they have always been a good reference for me.
Lastly, you can look at the TimeSpan class by JavaRanch's own Thomas Paul for some more guidance.
I hope that helps.
[ February 06, 2004: Message edited by: Mark Vender ]
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

Also, the discussion in this thread may prove useful (especially after reading the JavaAlmanac stuff)
I agree. Here's the link:
subject: compare dates
It's not a secret anymore!