如果字符串包含000,则OffsetDateTime不显示毫秒 [英] OffsetDateTime is not showing milisecond if the string contains 000

查看:265
本文介绍了如果字符串包含000,则OffsetDateTime不显示毫秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串"2020-03-25T22:00:00.000Z"我想将其转换为OffsetDateTime. 下面是我尝试的代码,但是当我将毫秒传递为000时,它不会反映在OffsetDateTime中.

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

但是当mili是000时

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"));

有人可以帮我吗

推荐答案

您必须记住,用于解析ISO-String的格式可能与用于将日期时间对象转换回String的格式不同.

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);

然后将调用OffsetDateTime.toString()方法,并从其文档中调用该方法:

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

将此日期时间输出为字符串,例如2007-12-03T10:15:30 + 01:00. 输出将是以下ISO-8601格式之一:

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

所使用的格式将是最短的,用于输出被省略部分隐含为零的时间的完整值.

同样,请记住,java.time软件包中的任何类都不会具有格式,所有这些类都包含一些表示毫秒或天等的long字段.

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.

我可能对此不太强调,但这是处理日期和时间时的本质:java.time没有具有格式,您需要转换

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.

因此,如果您始终希望拥有完整的价值,那么只需在打印前将OffsetDateTime格式化为String.

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);

哪个输出:

2020-03-25T22:00:00.000Z

这篇关于如果字符串包含000,则OffsetDateTime不显示毫秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆