4

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.

David Parks
  • 28,443
  • 44
  • 166
  • 295

2 Answers2

3

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).

khachik
  • 27,152
  • 7
  • 54
  • 92
0

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...

Guillaume
  • 13,867
  • 3
  • 42
  • 39