java 时区显示值与给定时区的 GMT 偏移量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15362738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Timezone display value with GMT offset from a given timezone?
提问by John
I would like to construct a timezone list to show it to users to select. The display name has to be like:
我想构建一个时区列表以显示给用户选择。显示名称必须是这样的:
( GMT 5:30 ) India Standard Time(Asia/Calcutta)
( GMT 5:30 ) India Standard Time(Asia/Calcutta)
I am taking all timezones with TimeZone.getAvailableIDs()
and constructing the list. The code I wrote is:
我正在使用所有时区TimeZone.getAvailableIDs()
并构建列表。我写的代码是:
String[] timeZones = TimeZone.getAvailableIDs();
List<String> tzList = new ArrayList<String>();
for (String timeZone : timeZones)
{
TimeZone tz = TimeZone.getTimeZone(timeZone);
StringBuilder timeZoneStr = new StringBuilder();
timeZoneStr.append("( GMT ").append(tz.getRawOffset() / (60 * 60 * 1000)).append(" ) ").append(tz.getDisplayName()).append("(").append(timeZone).append(")");
tzList.add(timeZoneStr.toString());
System.out.println(timeZoneStr.toString());
}
A snippet of the output would be like:
输出的片段如下:
( GMT 5 ) Maldives Time(Indian/Maldives)
( GMT 5 ) Pakistan Time(PLT)
( GMT 5 ) India Standard Time(Asia/Calcutta)
( GMT 5 ) India Standard Time(Asia/Kolkata)
( GMT 5 ) India Standard Time(IST)
But the output I need to get is:
但我需要得到的输出是:
( GMT 5:0 ) Maldives Time(Indian/Maldives)
( GMT 5:0 ) Pakistan Time(PLT)
( GMT 5:30 ) India Standard Time(Asia/Calcutta)
( GMT 5:30 ) India Standard Time(Asia/Kolkata)
( GMT 5:30 ) India Standard Time(IST)
What should I do to get 5:30?
我该怎么做才能得到 5:30?
回答by Duncan Jones
A more readable answer is the following:
更具可读性的答案如下:
for (String timeZone : timeZones) {
TimeZone tz = TimeZone.getTimeZone(timeZone);
long hours = TimeUnit.MILLISECONDS.toHours(tz.getRawOffset());
long minutes = TimeUnit.MILLISECONDS.toMinutes(tz.getRawOffset())
- TimeUnit.HOURS.toMinutes(hours);
String timeZoneString = String.format("( GMT %d:%02d ) %s(%s)", hours,
minutes, tz.getDisplayName(), timeZone);
tzList.add(timeZoneString);
System.out.println(timeZoneString);
}
This also correctly displays e.g. 5:00
and 5:30
. The use of String.format()
makes the final string easier to determine when reading the code. The use of the TimeUnit
class simplifies the maths.
这也正确显示例如5:00
和5:30
。使用String.format()
使阅读代码时更容易确定最终字符串。TimeUnit
类的使用简化了数学。
回答by Emmanuel Bourg
Another solution with the proper formatting :
具有正确格式的另一种解决方案:
DateFormat fmt = new SimpleDateFormat("HH:mm");
fmt.setTimeZone(TimeZone.getTimeZone("UTC"));
List<String> tzList = new ArrayList<String>();
for (String timeZone : TimeZone.getAvailableIDs()) {
TimeZone tz = TimeZone.getTimeZone(timeZone);
StringBuilder timeZoneStr = new StringBuilder();
timeZoneStr.append("( GMT ");
if (tz.getRawOffset() < 0) {
timeZoneStr.append("-");
}
timeZoneStr.append(fmt.format(new Date(Math.abs(tz.getRawOffset()))));
timeZoneStr.append(" ) ").append(tz.getDisplayName()).append(timeZone).append(")");
tzList.add(timeZoneStr.toString());
System.out.println(timeZoneStr.toString());
}
回答by Filipe Fedalto
You should switch the timezone forming line to:
您应该将时区形成线切换为:
timeZoneStr.append("( GMT ").append(tz.getRawOffset() / (60 * 60 * 1000)).append(":").append((tz.getRawOffset() / (60 * 1000))%60).append(" ) ").append(tz.getDisplayName()).append("(").append(timeZone).append(")");
I think this is the answer you're looking for. However, you may still format the minutes, as they only show one digit when the time offset is, for example 12 hours. It's shown as 12:0.
我想这就是你正在寻找的答案。但是,您仍然可以格式化分钟,因为当时间偏移为 12 小时时,它们只显示一位数字。它显示为 12:0。
回答by Nandan Tadahal
Offset timezone using below method :
使用以下方法偏移时区:
private static String displayTimeZone(TimeZone tz) {
long hours = TimeUnit.MILLISECONDS.toHours(tz.getRawOffset());
long minutes = TimeUnit.MILLISECONDS.toMinutes(tz.getRawOffset())
- TimeUnit.HOURS.toMinutes(hours);
// avoid -4:-30 issue
minutes = Math.abs(minutes);
String result = "";
if (hours > 0) {
result = String.format("(GMT+%d:%02d) %s", hours, minutes, tz.getID());
} else {
result = String.format("(GMT%d:%02d) %s", hours, minutes, tz.getID());
}
return result;
}
回答by Matthias M.
Just use a SimpleDateFormat
with the XXX
timezone pattern (produces an ISO 8601 timezone)
只需将 aSimpleDateFormat
与XXX
时区模式一起使用(生成 ISO 8601 时区)
DateFormat gmt = new SimpleDateFormat("'GMT' XXX");
for (TimeZone tz : timeZones) {
gmt.setTimeZone(tz);
String gmtTimezone = gmt.format(new Date());
System.err.println(tz.getID() + " " + gmtTimezone);
}
Source: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html#iso8601timezone
来源:http: //docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html#iso8601timezone