David Parks
David Parks

Reputation: 32081

How to produce a good dropdown list of timezones that correspond to java TimeZone(s)

What strategy do other webapps use to generate a nicely formatted list of timezones for user preferences?

I tried just getting all the time zones, but the list is long and not exactly formatted well for a user.

Just want to know how other people are doing this.

Upvotes: 4

Views: 4878

Answers (2)

khachik
khachik

Reputation: 28703

The following code snippet

...
String [] ids = TimeZone.getAvailableIDs();
for(String id:ids) {
  TimeZone zone = TimeZone.getTimeZone(id);
  int offset = zone.getRawOffset()/1000;
  int hour = offset/3600;
  int minutes = (offset % 3600)/60;
  System.err.println(String.format("(GMT%+d:%02d) %s", hour, minutes, id));
}   
...

will print formatted time zones like:

(GMT+12:00) Pacific/Tarawa
(GMT+12:00) Pacific/Wake
(GMT+12:00) Pacific/Wallis
(GMT+12:45) NZ-CHAT

You might want to add filtering for different offsets and/or time zones given from zone.getDisplayName(zone.useDaylightTime(), TimeZone.SHORT).

Upvotes: 3

Guillaume
Guillaume

Reputation: 14671

An easy way is to put the time zones IDs in the list instead of the time zones themselves. TimeZone.getAvailableIDs() returns values such as GMT, Europe/Paris, America/New_York...

Upvotes: 0

Related Questions