• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calendar and Time Zones  RSS feed

 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably a beginner question *mutters* but I thought I would CONSIDER putting it here, just for you Cindy
Anyway, I have the following code:

And I get the following output:
Time: 90000
OFF: 0
TZ: Greenwich Mean Time
17:01:30
I'm in the MST time zone, which is why I'm getting that 17, instead of 00. My question is, WHY!?!
If it is showing that the Calendar instance I'm using has on offset of 0, is using GMT time zone, then shouldn't I be returning 00:01:30?
What I'm trying to do is given a delta time in milliseconds, print out the number of hours, minutes and seconds of that delta time in a specified format. I could easily write out the computation to get all that, but would be nice if I could use a Calendar and formatter.
Thanks!
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since the quote below points out that SimpleDateFormat has it's own Calendar object, and that is using the default time zone of your PC, I expect that is what you are seeing instead of the time zone of YOUR calendar object.
Workaround
If you are converting a Calendar object from your timezone to GMT:
begin.add(Calendar.MILLISECOND, (-1) * begin.get(begin.ZONE_OFFSET));

Evaluation
GregorianCalendar keeps local time consistent at each GMT offset. (Note that
SimpleDateFormat has its own Calendar object that has the default TimeZone.)

Sun Bug Parade
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aaaaah, that explains it then. Didn't even think about having to set the time zone on the formatter... kinda wacky.
Anyway, new code works:

Thank ya Cindy!
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, now the question is can anyone see a reason NOT to do things this way, and instead just do the calculations?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!