以下作品(显示 UTC 时间)
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
System.out.println(new Date());
但这不是(显示本地时间)
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
System.out.println(cal.getTime());
System.out.println(new Date());
有什么简单的东西是我遗漏的吗?
最佳答案
您正在打印 Date.toString()
的结果,始终使用默认时区。
我建议您使用DateFormat
相反,它更适合格式化日期。 Date.toString
实际上只适合调试 - 它不提供对格式的控制。
或者,使用 Joda Time对于所有日期和时间操作 - 这是一个更好的 API 开始:)
关于Java日历: getting time for the timezone,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7482956/