OffsetDateTime is not showing milisecond if the string contains 000 [duplicate]

你。 提交于 2021-01-29 08:52:23

问题


I have a string "2020-03-25T22:00:00.000Z" which i want to convert to OffsetDateTime. Below is the code I tried but when I pass milisecond as 000 then it is not reflecting in 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

But when mili is 000 then

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)

I tried custom formattter also but it behave same

OffsetDateTime.parse("2020-03-25T22:00:00.123Z",
           DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX"));

Can anyone please help me out


回答1:


You have to remember that the format you used to parse the ISO-String may not be the same format used for converting the date-time object back to a String.

When you print the offset date time like this:

System.out.println(odt);

Then the OffsetDateTime.toString() method will be called, and from its documentation:

Outputs this date-time as a String, such as 2007-12-03T10:15:30+01:00. The output will be one of the following ISO-8601 formats:

  • uuuu-MM-dd'T'HH:mmXXXXX
  • uuuu-MM-dd'T'HH:mm:ssXXXXX
  • uuuu-MM-dd'T'HH:mm:ss.SSSXXXXX
  • uuuu-MM-dd'T'HH:mm:ss.SSSSSSXXXXX
  • uuuu-MM-dd'T'HH:mm:ss.SSSSSSSSSXXXXX

The format used will be the shortest that outputs the full value of the time where the omitted parts are implied to be zero.

Again, keep in mind that any class in the java.time package will not have a format, all those classes consist of some long fields which represent milliseconds or days etc.

I possibly can't stress this enough, but that is the essence when working with date and time: java.time classes do not have a format, you need to convert them into an appropriate format.

So if you always want to have the full value, then you simply need to format the OffsetDateTime to a String before you print it.

// best is to store that in a static variable
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX");

// parse using our custom formatter
OffsetDateTime odt = OffsetDateTime.parse("2020-03-25T22:00:00.000Z", dtf);

// print using our custom formatter
String formatted = odt.format(dtf);
System.out.println(formatted);

Which outputs:

2020-03-25T22:00:00.000Z



回答2:


I ran into the same issue a while ago, and there is actually an open thread on the Jackson module GitHub page here: https://github.com/FasterXML/jackson-modules-java8/issues/76

As you can see from this thread it seems to be an issue in the design of the Jackson module itself.

You should not be worried that the milliseconds are missing. It is designed to omit trailing zeroes in the ISO string to save time and memory, I think.

In my opinion this should be customizable behaviour.

You can find the exact same question and problem here: https://stackoverflow.com/a/52191521/4697963

Worth having a look and reading that thread.



来源:https://stackoverflow.com/questions/62890384/offsetdatetime-is-not-showing-milisecond-if-the-string-contains-000

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!