我有一个使用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 lib
最佳答案
万一有人遇到类似问题,我将留下答案。我意识到,由于我要覆盖默认的DateTime
序列化程序,因此我需要对自定义序列化程序进行以下更改以处理ZonedInstant
case JString(s) ⇒ Try(dateTimeFormat.parseDateTime(s)).getOrElse({
val zonedInstant = DateParser.parse(s, format)
new DateTime(zonedInstant.instant, DateTimeZone.forTimeZone(zonedInstant.timezone))
})