Java 将 DateFormat 时区设置为 GMT+1
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14940239/
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
Java set DateFormat timezone to GMT+1
提问by Rui
What is the correct string to set the DateFormat timezone to GMT+1? According the documentation it should be something like "GMT + 00:00". I already tried other forms but apparently I always fallback to GMT (my current timezone).
将 DateFormat 时区设置为 GMT+1 的正确字符串是什么?根据文档,它应该类似于“GMT + 00:00”。我已经尝试过其他形式,但显然我总是退回到格林威治标准时间(我当前的时区)。
Thanks in advance!
提前致谢!
回答by Jon Skeet
You can use
您可以使用
TimeZone fixedUtcPlus1 = new SimpleTimeZone(TimeUnit.HOURS.toMillis(1),
"GMT+1");
format.setTimeZone(fixedUtcPlus1);
Or just:
要不就:
TimeZone zone = TimeZone.getTimeZone("GMT+1");
format.setTimeZone(zone);
(Apologies for the repeated edits around +1 and -1... bad diagnostics on my part. "GMT+1" is fine, but its Etc equivalent is "Etc/GMT-1" - very confusing.)
(为 +1 和 -1 的重复编辑道歉......我的诊断错误。“GMT+1”很好,但它的 Etc 等价物是“Etc/GMT-1”——非常混乱。)
回答by Raman
You can find whole set of time zones via following fragment of code:
您可以通过以下代码片段找到整套时区:
for (String id : TimeZone.getAvailableIDs()) {
System.out.println(id);
}
And reuse it to set timezone directly:
并重用它直接设置时区:
DateFormat df = DateFormat.getDateInstance();
df.setTimeZone(TimeZone.getTimeZone(id));