aspose file tools*
The moose likes Beginning Java and the fly likes Adjusting GregorianCalendar to reflect a specific TimeZone Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Adjusting GregorianCalendar to reflect a specific TimeZone " Watch "Adjusting GregorianCalendar to reflect a specific TimeZone " New topic
Author

Adjusting GregorianCalendar to reflect a specific TimeZone

Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
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!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18909
    
    8

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

Joined: Nov 06, 2000
Posts: 177
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
Bartender

Joined: Oct 14, 2005
Posts: 18909
    
    8

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adjusting GregorianCalendar to reflect a specific TimeZone