在我的机器中,设置的时区是(UTC)协调世界时。但下面的示例代码返回巴基斯坦时间和+5:00
private void Foo()
{
DateTimeFormatter dtf = DateTimeFormat.forPattern("ZZ");
DateTime dt = DateTime.now();
System.out.println(dtf.print(dt)); // Prints +05:00, though expected is +00:00
DateTimeZone tz = DateTimeZone.getDefault();
System.out.println(" Get Name " + tz.getName(System.currentTimeMillis()));
// Prints Get Name Pakistan Time , although expected is Coordinated Universal Time
}
最佳答案
您的计算机可能设置为 UTC,但 JVM 也可能设置为其他时间。
来自Joda Time docs在 DateTimeZone.getDefault()
上:
The default time zone is derived from the system property user.timezone. If that is null or is not a valid identifier, then the value of the JDK TimeZone default is converted. If that fails, UTC is used.
因此,您可以在启动时将时区传递给 JVM:
java -Duser.timezone="UTC"
或者可以在TZ
环境变量中设置系统时区等。参见this answer了解更多详情。
关于java - Joda DateTimeZone.getName(long instant) 返回无效名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19382016/