tags:

views:

783

answers:

5

I am allowing users on my web app to schedule events based on time zones of their choice.

I want to present a good list of time zones to the end user and then convert it easily to java.util.TimeZone object at the server end.

String[] TimeZone.getAvailableIds() is something I could use, but the issue is that it prints about 585 time zone ids.

What is the best way to present to the user a brief list of time zones (like a Windows box would for time zone settings) and easily convert to TimeZone object at server end using its id?

A: 

Couldn't you use a list of custom time zone IDs using "GMT +/- Hours" notation (skipping minutes)?

(EDIT: With my first suggestion, daylight saving time transition is not automatic. To address this issue, you could first ask the user to select a GMT offset and then show a (linked) list of time zone IDs for the given offset using:

public static String[] getAvailableIDs(int rawOffset)

This way, the user would be able choose his time zone in a shorter list (better for user experience) and benefit from daylight savings behavior.)

Pascal Thivent
Thanks.Yes, But the java.util.TimeZone that would be created with it would not take into account daylight saving time automatically. If I create time zone using: TimeZone.getTimeZone("America/Los_Angeles"), then I dont need to worry about DST etc. Or am I mistaken?
Vish
You're right. Indeed, no daylight saving time transition with a custom time zone ID. I'll update my answer with another suggestion.
Pascal Thivent
Thanks. This works too!
Vish
You're welcome. Thanks for the upvote ;)
Pascal Thivent
A: 

If you need the granularity of choosing exactly how the list would look, I would use the best hard-coded list I could find (this is a good example) and ensure it is displayed and converted as precisely as possible.

Just keep in mind that each one of those 585 time zones does have a semantic meaning (such as DST for example) and users might want to choose the best time zone for them. Although I do agree that list can be much shorter.

Yuval A
+2  A: 

The list of timezones is very application and locale specific. Only you know what zones are most applicable to your users. We actually have different lists for different regions.

Here is our list for US users for your reference,

    "Pacific/Midway",
    "US/Hawaii",
    "US/Alaska",
    "US/Pacific",
    "America/Tijuana",
    "US/Arizona",
    "America/Chihuahua",
    "US/Mountain",
    "America/Guatemala",
    "US/Central",
    "America/Mexico_City",
    "Canada/Saskatchewan",
    "America/Bogota",
    "US/Eastern",
    "US/East-Indiana",
    "Canada/Eastern",
    "America/Caracas",
    "America/Manaus",
    "America/Santiago",
    "Canada/Newfoundland",
    "Brazil/East",
    "America/Buenos_Aires",
    "America/Godthab",
    "America/Montevideo",
    "Atlantic/South_Georgia",
    "Atlantic/Azores",
    "Atlantic/Cape_Verde",
    "Africa/Casablanca",
    "Europe/London",
    "Europe/Berlin",
    "Europe/Belgrade",
    "Europe/Brussels",
    "Europe/Warsaw",
    "Africa/Algiers",
    "Asia/Amman",
    "Europe/Athens",
    "Asia/Beirut",
    "Africa/Cairo",
    "Africa/Harare",
    "Europe/Helsinki",
    "Asia/Jerusalem",
    "Europe/Minsk",
    "Africa/Windhoek",
    "Asia/Baghdad",
    "Asia/Kuwait",
    "Europe/Moscow",
    "Africa/Nairobi",
    "Asia/Tbilisi",
    "Asia/Tehran",
    "Asia/Muscat",
    "Asia/Baku",
    "Asia/Yerevan",
    "Asia/Kabul",
    "Asia/Yekaterinburg",
    "Asia/Karachi",
    "Asia/Calcutta",
    "Asia/Colombo",
    "Asia/Katmandu",
    "Asia/Novosibirsk",
    "Asia/Dhaka",
    "Asia/Rangoon",
    "Asia/Bangkok",
    "Asia/Krasnoyarsk",
    "Asia/Hong_Kong",
    "Asia/Irkutsk",
    "Asia/Kuala_Lumpur",
    "Australia/Perth",
    "Asia/Taipei",
    "Asia/Tokyo",
    "Asia/Seoul",
    "Asia/Yakutsk",
    "Australia/Adelaide",
    "Australia/Darwin",
    "Australia/Brisbane",
    "Australia/Sydney",
    "Pacific/Guam",
    "Australia/Hobart",
    "Asia/Vladivostok",
    "Asia/Magadan",
    "Pacific/Auckland",
    "Pacific/Fiji",
    "Pacific/Tongatapu",
ZZ Coder
A: 

I did this for a company I don't own any of anymore, so can't provide code. The JVM on Windows comes with a file called tzmappings (look in C:\Program Files\Java\jre6\lib or similar) which maps Windows timezones to Java's zoneinfo-based Continent/City form.

Unfortunately, the textual names in tzmappings are terrible, so you need to do a few minutes of tabulation. Open regedit and navigate to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones. Under this is a key for each timezone on the machine; Windows 7 has about 90. Each key has a value called Display which is the textual name you want; look for the key itself in tzmappings to find the Java time zone identifier for each one.

Andrew Duffy
A: 

With that many, I wouldn't try to shoehorn them into a select box list.... I'd put them on a list in a separate modal dialog (or popup, if you must), let the user scroll through and click the name they want. They would click on a link in the modal dialog, and it would populate a text field with the correct code, and you could then submit that to your server.

Better yet, have them click their location on a map of the world, and use an image map to translate that location to the appropriate time zone.

Spike Williams