我已经将日期时间切换为 threeten,但我仍然有一个第 3 方工具使用 joda 将带有时区的时间戳写入数据库,我需要从一个转换为另一个。 最好的方法是什么? 作为一种解决方法,我尝试了 DateTime.parse(zdt.toString) 但它失败了,因为 joda 不喜欢区域格式
格式无效:“2015-01-25T23:35:07.684Z[欧洲/伦敦]”在“[欧洲/伦敦]”处格式错误
最佳答案
请注意,使用 DateTimeZone.forID(...) 并不安全,这可能会引发 DateTimeParseException,因为通常 ZoneOffset.UTC 的 ID“Z”无法被 DateTimeZone 识别。
为了将 ZonedDateTime 转换为 DateTime,我推荐的是:
return new DateTime(
zonedDateTime.toInstant().toEpochMilli(),
DateTimeZone.forTimeZone(TimeZone.getTimeZone(zonedDateTime.getZone())));
关于java - 如何从 ZonedDateTime 转换为 Joda DateTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28142915/