java - 对于某些时区,通过 JodaTime 和 java.time 设置 TimeZone 后的更正时间给出了不同的结果,为什么?

标签 java datetime timezone jodatime java-time

以下是 java 中的一个小代码片段,尝试将以毫秒为单位的时间转换为可读的日期时间格式,

Long timeInMillis=1615806808301l; //2021-03-15T16:43:28.301+05:30 IST

String timeZone="Europe/Istanbul";

MutableDateTime  mdateTime  = new MutableDateTime(timeInMills);
mdateTime.setZone(DateTimeZone.forTimeZone(TimeZone.getTimeZone(timeZone)));

ZonedDateTime zdt = ZonedDateTime.ofInstant(Instant.ofEpochMilli(timeInMills), ZoneId.of(timeZone));

以下是 Joda[mdateTime] 和 Java.time[zdt] 对于相同的时间(单位:毫秒)和时区给出的结果,

Europe/Istanbul
2021-03-15T13:13:28.301+02:00[Europe/Istanbul]
2021-03-15T14:13:28.301+03:00[Europe/Istanbul]

Turkey
2021-03-15T13:13:28.301+02:00
2021-03-15T14:13:28.301+03:00[Turkey]

Europe/Moscow
2021-03-15T15:13:28.301+04:00
2021-03-15T14:13:28.301+03:00[Europe/Moscow]

Europe/Minsk
2021-03-15T14:13:28.301+03:00
2021-03-15T14:13:28.301+03:00[Europe/Minsk]

如您所见,对于某些时区,结果有所不同,

PS:我的实际意图不是将 timeInmillis 转换为可读的日期时间格式,而是了解为什么结果不同。

PS:系统时区为 IST[​​+05:30]

最佳答案

根据您显示的 Joda-Time 结果,您可能使用的是非常旧的版本。

时区随政府的一时兴起而变化。始终使用最新版本并掌握更新非常重要。

升级到当前版本的 Joda-Time(撰写本文时为 2.10.10),您报告的差异应该会消失。

关于java - 对于某些时区,通过 JodaTime 和 java.time 设置 TimeZone 后的更正时间给出了不同的结果,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66638082/

相关文章:

python - .json 扩展文件 + 时间戳 + Pandas + Python

java - 如何在java中将 float 和其他类型的值保存为字符

Java:如何在子进程中调用 prctl()

PHP转换日期格式

java - 指定 log4j 日期的时区

php - 使用 mysql 使用时区 php 查找打开和关闭状态

android - 如何在 Android 中获取 TimeZone 的名称

java - 需要帮助理解实例变量

java - 将现有的Cassandra数据集成到Elassandra

python - 带有来自日期时间向量的时间箱的直方图