我有一个使用Java JodaTime
的现有应用程序。但是,升级到最新的json4s-core
库 3.6.0-M3 后,我在将带有时区的日期字符串转换为日期时遇到以下错误。
Caused by: java.lang.IllegalArgumentException: No instant converter found for type: org.json4s.ext.DateParser$ZonedInstant
当我编写具有多种格式回退的自定义DateTime
序列化程序时,就会发生这种情况:
case JString(s) ⇒ Try(dateTimeFormat.parseDateTime(s)).getOrElse(new DateTime(DateParser.parse(s, format)))
导致问题的示例字符串:2018-05-02T21:43:29Z
我确保我使用的是 jodatime 2.9.2 和匹配的 json4s-ext 库
如果有人遇到类似的问题,我会留下我的答案。我意识到,由于我要重写默认的DateTime
序列化程序,因此我需要对自定义序列化程序进行以下更改以处理ZonedInstant
case JString(s) ⇒ Try(dateTimeFormat.parseDateTime(s)).getOrElse({
val zonedInstant = DateParser.parse(s, format)
new DateTime(zonedInstant.instant, DateTimeZone.forTimeZone(zonedInstant.timezone))
})