【发布时间】:2020-07-18 15:43:33
【问题描述】:
我想从数据库中检索日期并保留其格式。例如"20200406T145511.067Z" 当我尝试将其解析为 Joda DateTime 时,格式更改为 "2020-04-06T14:55:11.067+01:00"。由于这个问题,我试图明确说明 Joda DateTime 应该返回的 DateTimeFormat,"yyyyMMdd'T'HHmmss.SSS'Z'",但它似乎并不适用。我想以我期望的格式返回一个DateTime 对象(例如"20200406T145511.067Z")。
val dateTimeFormat = DateTimeFormat.forPattern("yyyyMMdd'T'HHmmss.SSS'Z'")
val itemDateTime = dateTimeFormat.parseDateTime(record.get("itemDateTime").asString)
println("Neo4J: " + record.get("assetDateTime").asString) // Neo4J: 20200406T145511.067Z
println("Joda: " + assetDateTime) // Joda: 2020-04-06T14:55:11.067+01:00
val lastUpdatedDateTime = dateTimeFormat.parseDateTime(record.get("lastUpdatedDateTime").asString)
println("Neo4J: " + record.get("lastUpdatedDateTime").asString) // Neo4J: 20200406T145511.383Z
println("Joda: " + lastUpdatedDateTime) // Joda: 2020-04-06T14:55:11.383+01:00
编辑:我已更新我的代码以返回正确的类型 DateTime,但我现在收到无效格式错误 -
java.lang.IllegalArgumentException: Invalid format: "20200406T161516.856Z" is malformed at "1516.856Z"
我不明白为什么会这样。任何帮助,将不胜感激。
更新代码:
val dateTimeFormat = DateTimeFormat.forPattern("yyyyMMdd'T'HHmmss.SSS'Z'")
val itemDateTime = dateTimeFormat.parseDateTime(record.get("itemDateTime").asString)
val itemDateTimeFormat = dateTimeFormat.print(itemDateTime)
val lastUpdatedDateTime = dateTimeFormat.parseDateTime(record.get("lastUpdatedDateTime").asString)
val lastUpdatedDateTimeFormat = dateTimeFormat.print(lastUpdatedDateTime)
if (lastUpdatedDateTime.isAfter(itemDateTime) new DateTime(lastUpdatedDateTimeFormat) else new DateTime(itemDateTimeFormat)
【问题讨论】:
-
这是目前工作中使用的。我们希望在未来进行更新,但现在我需要坚持下去。