File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

compare dates

 
Kristol Crawley
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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());
varCurrentCal.getTime();

varReqCal.set(year,(month -1),day,hour,minute);
varReqCal.getTime();
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
Posts: 624
IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kristol,
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 JavaAlmanac.com 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.
Regards,
Mark
[ February 06, 2004: Message edited by: Mark Vender ]
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, the discussion in this thread may prove useful (especially after reading the JavaAlmanac stuff)
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic