Java 8 LocalDateTime在解析带有00秒的日期字符串值(例如"2018-07-06 00:00:00")时会降低00秒值; [英] Java 8 LocalDateTime dropping 00 seconds value when parsing date string value with 00 seconds like "2018-07-06 00:00:00"

查看:715
本文介绍了Java 8 LocalDateTime在解析带有00秒的日期字符串值(例如"2018-07-06 00:00:00")时会降低00秒值;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码(Java 8)代码段在解析的日期内将秒值设为时,会删除我的日期时间中的秒部分使用 LocalDateTime.parse ,例如2018-07-10 00:00: 00 :

The code (Java 8) snippet below drops the seconds part of my date time when the seconds value is zero within the date parsed using LocalDateTime.parse, like 2018-07-10 00:00:00:

final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
final LocalDateTime localDateTime = LocalDateTime.parse("2018-07-06 00:00:00", dateTimeFormatter);
final String lexicalDate = localDateTime.toString();
System.out.println("Lexical Date : "+ lexicalDate);
final XMLGregorianCalendar gregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(lexicalDate);
System.out.println("Gregorian Calendar : "+ gregorianCalendar);

词汇日期打印为:

词汇日期:2018-07-10T00:00

Lexical Date : 2018-07-10T00:00

代替:

词汇日期:2018-07-10T00:00:00

Lexical Date : 2018-07-10T00:00:00

现在,这会影响公历的日期值,该公历在删除第二个时会返回空.其他情况下,如果秒值大于零,则效果很好.

Now this is affecting the date value of the gregorian calendar which returns null when the second is dropped. Other cases when the seconds value is greater than Zero, it works perfectly.

javax.xml.datatype.DatatypeFactory.newInstance().newXMLGregorianCalendar(lexicalDate)

每当由于解析的字符串带有00秒的值而导致秒值被丢弃时,以上代码将返回null.

The above code is returning null whenever the seconds value is dropped because of 00 seconds value withing the parsed string.

有人可以使用LocalDate时间提供更好的方法来解决此问题吗,否则它可能是Java 8 LocalDateTime中的错误/有趣的控件.

Can someone assist with a better way that handles this issue using LocalDate time, otherwise it might be a bug/funny control in Java 8 LocalDateTime.

请注意,我无法控制此日期值,它来自第三方平台.

Please note I do not have control over this date value, it's coming from a thirdy party platform.

推荐答案

功能而非错误

您正在看到节选,我的重点:

输出将为以下 ISO-8601 格式之一:

uuuu-MM-dd'T'HH:mm

uuuu-MM-dd'T'HH:mm

uuuu-MM-dd'T'HH:mm:ss

uuuu-MM-dd'T'HH:mm:ss

uuuu-MM-dd'T'HH:mm:ss.SSS

uuuu-MM-dd'T'HH:mm:ss.SSS

uuuu-MM-dd'T'HH:mm:ss.SSSSSS

uuuu-MM-dd'T'HH:mm:ss.SSSSSS

uuuu-MM-dd'T'HH:mm:ss.SSSSSSSSS

uuuu-MM-dd'T'HH:mm:ss.SSSSSSSSS

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

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

如果在生成表示您的值的字符串时想要其他行为,请 LocalDateTime ,使用其他 DateTimeFormatter 并将其传递给

If you want other behavior when generating a String to represent the value of you LocalDateTime, use a different DateTimeFormatter and pass it to LocalDateTime::format.

String output = myLocalDateTime.format( someOtherFormatter ) ;

LocalDateTime没有格式",因为它不是不是文本. DateTimeFormatter的工作是解析或生成特定格式的String 对象.

The LocalDateTime has no "format" as it is not text. It is the job of the DateTimeFormatter to parse or generate String objects of a particular format.

这篇关于Java 8 LocalDateTime在解析带有00秒的日期字符串值(例如"2018-07-06 00:00:00")时会降低00秒值;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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