Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Calendar and Time Zones

 
Chicken Farmer ()
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!
 
"The Hood"
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 ()
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 ()
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?
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic