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:
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.
Joined: Nov 06, 2000
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.
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.