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

convert datetime from EST to PST

 
Dave Bosky
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to convert a date time from EST to PST time.
ex: 3/24/2004 09:00:00 -> 3/24/2004 06:00:00
I thought I could just subtract 3 hours and be done but the date could change after subtracting 3 hours from 1AM (01:00:00) and that threw in another problem...
Are any classes I that do something similiar to what I need?
Any examples would be excellent as well....
Thanks,
Dave
 
Jeremy Wilson
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe if you create a GregorianCalendar object you can specify the timezone that the object will return. Create the object using EST and then change the timezone to PST for the new value.
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you don't necessarily have to subtract 3 hours; just set the hours value to 3 less:

I think that should work. The whole mod thing is so if it turns out to be a negative number, we actually want the value set to 24 - value. See if this will work for you.
My only other suggestion is the use of java.util.TimeZone and it's methods to compute differences of times between different time zones and UTC; but it seems your non-change-of-day constraint makes some of this a bit akward.
 
chi Lin
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can represent the EST date object
in term of mill seconds then use
setTimeInMillis(currentTimeMill - 60*60*3000) to get the PST time.
this way, the new date will adjust itself automatically.
[ March 25, 2004: Message edited by: chi Lin ]
 
Dave Bosky
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I need the date to change with the time.
If I've got 03/10/2004 01:00:00 EST and when converting to PST I need the date to change if necessary.
Example 03/10/2004 01:00:00 EST => 03/09/2004 22:00:00 PST.
So which class would be better to use timezone or gregorian calendar?

Thanks Again,
Dave
 
chi Lin
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you only care about EST to PST, GregorianCalendar is enough
try the following:

[ March 25, 2004: Message edited by: chi Lin ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic