Win a copy of Getting started with Java on the Raspberry Pi this week in the Raspberry Pi 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

Question about using a specific TimeZone to set a GregorianCalendar...

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I am trying to do is create a Gregorian calendar and set the time to
one second before Daylight Saving Time begins, and print it out. Then I
want to reset the time to 2:00 am and see if it realizes it is now in EDT.

Okay... so here's my sample code:
The output looks like the following:

Current date/time = Sat Mar 07 20:59:59 EST 2009

Clearly this is EST, which is good, but it's -5 hours back
from the time I wanted to set. Of cours if I substitute the
following line for the calendar.set line above:


Then the output is:

Current date/time = Sun Mar 08 01:59:59 EST 2009

Of course I don't think I should have to do that.
I realize I am using the parent Calendar set()
method to do this, but shouldn't it use the same
TimeZone I set in the constructor? Do I have to
think in the GMT time, or is there a way to make
this set work as I imagined it?

Thanks,
DJ

 
Marshal
Posts: 27900
94
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You set the timezone of the Calendar object. So far so good. But then you extracted a Date object from that Calendar (via the getTime() method), and Date knows nothing of time zones. So already you've lost the time zone information. Then you print the Date; what time zone do you think it uses? It can't be the time zone belonging to the Calendar object, because the Date doesn't know or care what that was. No, it's your system's default time zone. Which presumably isn't Americas/New_York.

The way to fix that is to use a SimpleDateFormat object to format the Date for output. And to set its timezone to Americas/New_York.
 
Don Smith
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That response definitely helped me find my mistake...

My default timezone is "America/New_York", NOT "Americas/New_York".

Once I used a valid time zone ID, it worked fine...

Thanks!
 
It's fun to be me, and still legal in 9 states! Wanna see my tiny ad?
Low Tech Laboratory
https://www.kickstarter.com/projects/paulwheaton/low-tech-0
reply
    Bookmark Topic Watch Topic
  • New Topic