Java将DateFormat时区设置为GMT + 1

时间:2013-02-18 15:57:47

标签: java timezone simpledateformat

将DateFormat时区设置为GMT + 1的正确字符串是什么? 根据文件,它应该像“GMT + 00:00”。 我已经尝试了其他形式,但显然我总是回到GMT(我目前的时区)。

提前致谢!

2 个答案:

答案 0 :(得分:8)

您可以使用

TimeZone fixedUtcPlus1 = new SimpleTimeZone(TimeUnit.HOURS.toMillis(1),
                                            "GMT+1");
format.setTimeZone(fixedUtcPlus1);

或者只是:

TimeZone zone = TimeZone.getTimeZone("GMT+1");
format.setTimeZone(zone);

(对于+1和-1重复编辑的道歉......我的错误诊断。“GMT + 1”很好,但其Etc等价物是“Etc / GMT-1” - 非常令人困惑。)< / p>

答案 1 :(得分:5)

您可以通过以下代码片段找到整套时区:

for (String id : TimeZone.getAvailableIDs()) {
    System.out.println(id);
}

并重复使用它来直接设置时区:

DateFormat df = DateFormat.getDateInstance();
df.setTimeZone(TimeZone.getTimeZone(id));