ZonedDateTime忽略SS部分的00 [英] ZonedDateTime ignores 00 for ss part

查看:257
本文介绍了ZonedDateTime忽略SS部分的00的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将字符串值转换为"2018-10-11T12:00:00Z";到ZonedDateTime. 如果我使用默认的ZonedDateTime.parse(zonedDateTime),则会删除ss部分,即00,并得到"2018-10-11T12:00Z"作为输出.

I am trying to convert string value "2018-10-11T12:00:00Z"; to ZonedDateTime. If I use the default ZonedDateTime.parse(zonedDateTime) then ss portion i.e. 00 is removed and I get "2018-10-11T12:00Z" as output.

请问如何保留00作为SS部分.

Can you please help how to retain 00 as SS part.

String zonedDateTime = "2018-10-11T12:00:00Z";

ZonedDateTime z =  ZonedDateTime.parse(zonedDateTime);
System.out.println(z);  // return 2018-10-11T12:00Z

ZonedDateTime z1 =  ZonedDateTime.parse (zonedDateTime, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ") );

System.out.println(z1);  // Exception : Text '2018-10-11T12:00:00Z' could not be parsed at index 19

推荐答案

打印秒数

仅使用System.out.println打印ZonedDateTime时,将隐式调用其toString方法.当秒和秒的小数均为0时,toString将其保留.无论如何要打印它们,请使用格式化程序进行格式化(而不是使用格式化程序进行解析):

Printing the seconds

When you just print a ZonedDateTime using System.out.println, you are implicitly calling its toString method. When the seconds and fraction of second are 0, toString leaves them out. To print them anyway, use a formatter for formatting (as opposed to using one for parsing):

    DateTimeFormatter formatterWithSeconds = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ssXXX");
    System.out.println("z with seconds: " + z.format(formatterWithSeconds));

输出为:

z秒:2018-10-11T12:00:00Z

z with seconds: 2018-10-11T12:00:00Z

此处的重要区别是ZonedDateTime对象及其字符串表示形式之间的区别. ZonedDateTime对象总是包含秒和纳秒,当它们均为0时.字符串表示形式可能包含也可能不包含它们.一个参数ZonedDateTime.parse已解析00秒.顺便说一句,它接受带和不带秒的字符串.

The important distinction here is between the ZonedDateTime object and its string representation. The ZonedDateTimeobject always contains seconds and nanoseconds, also when they are 0. The string representation may or may not include them. The one-arg ZonedDateTime.parse has parsed the 00 seconds. BTW, it accepts strings both with and without seconds.

顺便说一句,由于您的字符串包含偏移量(Z)且没有时区(例如非洲/内罗毕),因此OffsetDateTime可以更精确地匹配表示形式.如果您始终用OffsetDateTime搜索/替换ZonedDateTime,则代码会正常工作,因为API相似.

As an aside, since your string contains an offset (Z) and no time zone (like Africa/Nairobi), an OffsetDateTime matches more precisely as representation. The code will work fine if you just search/replace ZonedDateTime with OffsetDateTime throughout since the APIs are similar.

如果偏移量始终为Z(对于祖鲁时区"或UTC偏移量为0),请使用Instant:

If the offset is always Z (for "Zulu time zone" or offset 0 from UTC), use Instant:

    String instantString = "2018-10-11T12:00:00Z";
    Instant i = Instant.parse(instantString);
    System.out.println("As Instant: " + i);

我为您的String变量指定了一个更适合此代码段的名称;它仍然是相同的字符串.输出:

I have given your String variable a name that is more appropriate for this snippet; it’s still the same string. Output:

即时:2018-10-11T12:00:00Z

As Instant: 2018-10-11T12:00:00Z

奇怪的是,即使它们为0,Instant.toString也会打印秒.

Curiously Instant.toString does print the seconds even when they are 0.

请阅读更多有关@Basil Bourque答案中使用的正确类型的信息.

Please read more about the correct type to use in the answer by @Basil Bourque.

// Exception : Text '2018-10-11T12:00:00Z' could not be parsed at index 19

这里的问题不在于秒,也不在于ss.索引19是Z在您的字符串中的位置.两个参数ZonedDateTime.parse的对象,因为模式字母Z与日期时间字符串中的Z不匹配.如果确实需要提供用于解析Z的格式模式字符串,请使用大写的X(取决于非零偏移的外观,使用大写字母X).从模式字母的文档中:

The problem here is not with the seconds, nor with the ss. Index 19 is where the Z is in your string. The two-arg ZonedDateTime.parse objects because pattern letter Z does not match a Z in the date-time string. If you do need to supply a format pattern string for parsing Z, use uppercase X (one or more of them depending on how a non-zero offset looks). From the documentation of pattern letters:

  Symbol  Meaning                     Presentation      Examples
  ------  -------                     ------------      -------
   X       zone-offset 'Z' for zero    offset-X          Z; -08; -0830; -08:30; -083015; -08:30:15
   Z       zone-offset                 offset-Z          +0000; -0800; -08:00

链接

  • DateTimeFormatter文档
  • 相关问题:解析Java中的日期字符串
  • Links

    • DateTimeFormatter documentation
    • Related question: Parse Date String in Java
    • 这篇关于ZonedDateTime忽略SS部分的00的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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