This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Darcy DeClute's Scrum Master Certification Guide: The Definitive Resource for Passing the CSM and PSM Exams and have Darcy DeClute on-line!
See this thread for details.
  • 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Adjusting GregorianCalendar to reflect a specific TimeZone

 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to get times, adjusted to a user-specified time zone. I.e., when I create a new GregorianDate, I want it to reflect the current time, adjusted for that time zone.

For example, assume the system clock is on central time, and it's noon in the central time zone. If the user specifies that they are in the eastern time zone, I want a new GregorianCalendar object set to 1:00 pm, not noon. (The system time is out of my control.)

I read the GregorianCalendar documentation, and I thought it would be a matter of passing in a TimeZone to its constructor, but that seems to have no effect.



In this example, I just ran it and the console shows:

So passing those TimeZone object to the GregorianCalendar constructor doesn't seem to do anything.

Help! And thanks!
 
Marshal
Posts: 28005
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
The step you missed is to configure the DateFormat object to display timestamps in your chosen timezone. Because you didn't do that, it displays them in your default timezone. That's nothing to do with whether the place it got those timestamps was configured to use any particular timezone.
 
Max Rahder
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Setting the format TimeZone works (thanks!), but I'm still vague on what the GregorianCalendar(TimeZone) parameter does. For example, this code shows the same sets of times for the two GregorianCalendar objects, even though they were created using two different time zones.



 
Paul Clapham
Marshal
Posts: 28005
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
When you set the timezone for a Calendar object, then any arithmetic you do (e.g. adding 6 days) will be done with respect to that timezone -- in particular with respect to its daylight saving time settings. That's what it's for. When you use the DateFormat.format() method, it's working with a Date object which was extracted from the Calendar. A Date object doesn't have any concept of timezone so the DateFormat object won't know anything about the timezone the Calendar was using.
 
Space seems cool in the movies, but once you get out there, it is super boring. Now for a fascinating tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic