我正在尝试更新我现有的 elasticsearch springboot 项目,由于源代码相当旧,它仍然使用 joda 时间。现在我要把Joda时间的所有功能升级为java时间。现在在项目中我们使用 Joda Time 的 Date Time
Joda Time 的代码示例
DateTime target = new DateTime(String targetDate, UTC);
Run Code Online (Sandbox Code Playgroud)
我们当前在代码中使用此函数将字符串转换为日期。使用此函数的字符串
2022-10-01T00:00:00.000
被转换为
2022-10-01T00:00:00.000Z
我正在尝试在 java 时间中复制相同的内容。
我尝试解析targetDateusing OffsetDateTimeandZonedDateTime但都给了我错误。
无法在索引 24 处解析文本“2022-10-01T00:00:00.0000”
经过一番尝试后,我能够通过使用继续前进LocalDateTime
LocalDateTime target = LocalDateTime.parse(String targetDate);
Run Code Online (Sandbox Code Playgroud)
它能够解析字符串,但格式不正确,我得到的格式是
2022-10-01T00:00Z
我还尝试将格式化程序与 LocalDateTime 一起使用
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSS'Z'");
LocalDateTime target= LocalDateTime.parse(targetDate,formatter);
Run Code Online (Sandbox Code Playgroud)
但这仍然给了我错误
无法在索引 24 处解析文本“2022-10-01T00:00:00.0000”
现在我对此有点困惑。
任何帮助表示赞赏。如果我提出问题或格式的方式在任何时候都是错误的,请纠正我。
问候。
编辑:抱歉造成混乱,但正如所指出的,我应该提到我希望返回的值作为java.time 日期时间对象而不是字符串,以便我可以进一步对其执行一些逻辑。非常遗憾。
感谢致敬