tags:

views:

1466

answers:

4

I'm having weird problem with java TimeZone..

Calling TimeZone.getDefault() gives my local time zone, which has an ID "GMT+02:00". Funny thing is that this ID doesn't appear in a list provided by TimeZone.getAvailableIDs(). Apparently my zone appears to be "Etc/GMT+2".

I'm trying to populate a combo with time zones, but it's impossible to put a selection because GMT+02:00 is not in the list.. Anyone seen this problem? Any ideas?


Update:

The bottom line is - can't rely on ID strings, must go with the offset as display ID's may vary from system to system.

A: 

Any timezone can be specified as "GMT" plus/minus an offset. The timezone doc refers to this as a "custom ID".

To populate a drop-down, I think you'd be better off coming up with a specific list of cities/offsets, with an association to the timezone. The array returned by getAvailableIDs() is huge -- 586 entries in my install -- and you definitely don't want to force your users to plod through this.

kdgregory
Yes, the array is large, but it's not a problem. I display it sorted and it's a snap to navigate to the relevant entry. My problem is not about the size, but about confusing way the ID's are represented.
Dima
+1  A: 

Looking back on some other SO questions, this seems to be a Java-on-Windows problem. Please have a look at this article, especially answer 1, which points to a wikipedia link that may solve your mapping issue.

Andreas_D
+1  A: 

How about using TimeZone.getAvailableIDs() and group them by the part before the slash and make it a two step selection?

fforw
+3  A: 

GMT+02:00 is a custom ID, it won't appear in the output of TimeZone.getAvailableIDs() (which is huge). What you could do is to ask the user to specify his offset first and then get the available IDs for the given offset. For example, for GMT+02:00, the following piece of code:

for (String string : TimeZone.getAvailableIDs(TimeZone.getTimeZone(
        "GMT+02:00").getRawOffset())) {
    System.out.println(string);
}

gives the following output:

ART
Africa/Blantyre
Africa/Bujumbura
Africa/Cairo
Africa/Gaborone
Africa/Harare
Africa/Johannesburg
Africa/Kigali
Africa/Lubumbashi
Africa/Lusaka
Africa/Maputo
Africa/Maseru
Africa/Mbabane
Africa/Tripoli
Asia/Amman
Asia/Beirut
Asia/Damascus
Asia/Gaza
Asia/Istanbul
Asia/Jerusalem
Asia/Nicosia
Asia/Tel_Aviv
CAT
EET
Egypt
Etc/GMT-2
Europe/Athens
Europe/Bucharest
Europe/Chisinau
Europe/Helsinki
Europe/Istanbul
Europe/Kaliningrad
Europe/Kiev
Europe/Mariehamn
Europe/Minsk
Europe/Nicosia
Europe/Riga
Europe/Simferopol
Europe/Sofia
Europe/Tallinn
Europe/Tiraspol
Europe/Uzhgorod
Europe/Vilnius
Europe/Zaporozhye
Israel
Libya
Turkey

It's still big but human browsable this time.

Pascal Thivent