我想从数据库中检索日期并保留其格式。例如"20200406T145511.067Z"
当我尝试将其解析为Joda DateTime时,格式更改为"2020-04-06T14:55:11.067+01:00"
。由于这个问题,我试图明确声明Joda DateTime应该返回为"yyyyMMdd'T'HHmmss.SSS'Z'"
的DateTimeFormat,但似乎不适用。我想以期望的格式(例如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)
yyyyMMdd'T'HHmmss.SSS'Z'
格式的字符串解析为有效的DateTime
对象。要以所需格式打印对象的内容,需要调用dateTimeFormat.print(itemDateTime)
import org.joda.time.DateTime
import org.joda.time.format.DateTimeFormat
import org.joda.time.format.ISODateTimeFormat
// val inputDt = record.get("itemDateTime").asString
// let's imagine we have a data as following:
val inputDt = "20200406T161516.856Z"
val dateTimeFormat = DateTimeFormat.forPattern("yyyyMMdd'T'HHmmss.SSS'Z'")
val itemDateTime = dateTimeFormat.parseDateTime(inputDt)
val parsedDate = DateTime.parse(itemDateTime.toString, ISODateTimeFormat.dateTimeParser());
println(parsedDate) // 2020-04-06T16:15:16.856Z
小样here