Get TimeZone offset value from TimeZone without TimeZone name

java, timezone

Solution

I need to save the phone's timezone in the format [+/-]hh:mm

No, you don't. Offset on its own is not enough, you need to store the whole time zone name/id. For example I live in Oslo where my current offset is +02:00 but in winter (due to dst) it is +01:00. The exact switch between standard and summer time depends on factors you don't want to explore.

So instead of storing `+ 02:00` (or should it be `+ 01:00`?) I store `"Europe/Oslo"` in my database. Now I can restore full configuration using:

TimeZone tz = TimeZone.getTimeZone("Europe/Oslo")

Want to know what is my time zone offset today?

tz.getOffset(new Date().getTime()) / 1000 / 60   //yields +120 minutes

However the same in December:

Calendar christmas = new GregorianCalendar(2012, DECEMBER, 25);
tz.getOffset(christmas.getTimeInMillis()) / 1000 / 60   //yields +60 minutes

Enough to say: store time zone name or id and every time you want to display a date, check what is the current offset (today) rather than storing fixed value. You can use `TimeZone.getAvailableIDs()` to enumerate all supported timezone IDs.

Problem

I need to save the phone's timezone in the format [+/-]hh:mm I am using TimeZone class to deal with this, but the only format I can get is the following: ``` PST -05:00 GMT +02:00 ``` I would rather not substring the result, is there any key or option flag I can set to only get the value and not the name of that timezone (GMT/CET/PST...)?

Original source

Related problems