How to produce a good dropdown list of timezones t

2019-04-10 04:25发布

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.

标签: java timezone
2条回答
男人必须洒脱
2楼-- · 2019-04-10 04:46

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

查看更多
干净又极端
3楼-- · 2019-04-10 04:49

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

查看更多
登录 后发表回答