如何生成与 java TimeZone(s) 对应的时区下拉列表 [英] How to produce a good dropdown list of timezones that correspond to java TimeZone(s)

查看:29
本文介绍了如何生成与 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.

推荐答案

以下代码片段

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

您可能希望为 zone.getDisplayName(zone.useDaylightTime(), TimeZone.SHORT) 中给出的不同偏移量和/或时区添加过滤.

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

这篇关于如何生成与 java TimeZone(s) 对应的时区下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆