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

标签 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

} 

最佳答案

您的计算机可能设置为 UTC,但 JVM 也可能设置为其他时间。

来自Joda Time docsDateTimeZone.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/

相关文章:

java - 如何使 Spring Joda-Time 格式化程序在非标准语言环境下工作?

java - 如何在java中使用Option类(cli-commons)

java - Grails 将日期时间保存为 UTC 时间,但将其读取为本地服务器时间?

java - 将 Hibernate 升级到 5.1.0 后如何导出模式?

python - Pandas 中的日期范围

Python/SQLite 按文本日期排序

java - 如何防止 JodaTime 期间的负面结果

datetime - 如何从 Scala 中的 joda-time DateTime 中减去一天/小时/分钟?

java - 当我运行 .jar 时,除命令提示符外,Swing 不会重新绘制

java - 基于 jtextfield 过滤 JTable 不区分大小写 (Java)