• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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!
 
Sheriff
Posts: 28326
96
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
Sheriff
Posts: 28326
96
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.
 
World domination requires a hollowed out volcano with good submarine access. Tiny ads are optional.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic