默认情况下,使用DateTimeFormatter.ISO_INSTANT格式化程序的toString方法.如果格式化程序碰巧为0,则不会打印秒数的数字.Instant
java时间示例:
2015-10-08T17:13:07.589Z
2015-10-08T17:13:07Z
Run Code Online (Sandbox Code Playgroud)
Joda-Time示例(以及我对java.time的期望):
2015-10-08T17:13:07.589Z
2015-10-08T17:13:07.000Z
Run Code Online (Sandbox Code Playgroud)
在一些系统中解析这真的很令人沮丧.Elasticsearch是我遇到的第一个问题,没有预定义的格式支持可选的millis,但我可以使用自定义格式解决这个问题.默认似乎是错误的.
看来你无法为Instants构建自己的格式字符串.是实现我自己的java.time.format.DateTimeFormatterBuilder.InstantPrinterParser的唯一选择吗?
import com.fasterxml.jackson.databind.util.ISO8601DateFormat;
objectMapper.setDateFormat(new ISO8601DateFormat());
Run Code Online (Sandbox Code Playgroud)
很好,但这忽略了毫秒,如何在不使用非线程安全的情况下在日期中获取它们SimpleDateFormatter?
我有一个字符串“2020-03-25T22:00:00.000Z”,我想将其转换为 OffsetDateTime。下面是我尝试过的代码,但是当我将毫秒作为 000 传递时,它不会反映在 OffsetDateTime 中。
OffsetDateTime offsetDateTime=OffsetDateTime.parse("2020-03-25T22:00:01.123Z",
DateTimeFormatter.ISO_OFFSET_DATE_TIME);
print(offsetDateTime)
//Output: 2020-03-25T22:00:01.123Z
Run Code Online (Sandbox Code Playgroud)
但是当mili为000时
OffsetDateTime offsetDateTime=OffsetDateTime.parse("2020-03-25T22:00:01.000Z",
DateTimeFormatter.ISO_OFFSET_DATE_TIME);
print(offsetDateTime)
//Output: 2020-03-25T22:01Z (mili second is missing)
Run Code Online (Sandbox Code Playgroud)
我也尝试了自定义格式化程序,但它的行为相同
OffsetDateTime.parse("2020-03-25T22:00:00.123Z",
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX"));
Run Code Online (Sandbox Code Playgroud)
谁能帮我一下吗