Joda DateTimeZone.getName(long instant)返回无效名称

时间:2013-10-15 13:05:05

标签: java datetime jodatime

在我的机器中,设置时区为(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

} 

1 个答案:

答案 0 :(得分:1)

您的计算机可能设置为UTC,但JVM可能设置为其他内容。

来自DateTimeZone.getDefault()上的Joda Time docs

  

默认时区源自系统属性user.timezone。如果该值为null或不是有效标识符,则转换JDK TimeZone缺省值。如果失败,则使用UTC。

因此,您可以在启动时将时区传递给JVM:

java -Duser.timezone="UTC"

或者您可以在TZ环境变量等中设置系统时区。有关详细信息,请参阅this answer