[英]Java 8 LocalDateTime dropping 00 seconds value when parsing date string value with 00 seconds like "2018-07-06 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 :下面的代码(Java 8)片段在使用LocalDateTime.parse解析的日期内秒值为零时删除我的日期时间的秒部分,例如 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);
The Lexical Date is printed as :词法日期打印为:
Lexical Date : 2018-07-10T00:00词法日期:2018-07-10T00:00
instead of :代替 :
Lexical Date : 2018-07-10T00:00:00词法日期:2018-07-10T00:00:00
Now this is affecting the date value of the gregorian calendar which returns null when the second is dropped.现在这会影响公历的日期值,当第二个被删除时它返回 null 。 Other cases when the seconds value is greater than Zero, it works perfectly.秒值大于零的其他情况下,它可以完美运行。
javax.xml.datatype.DatatypeFactory.newInstance().newXMLGregorianCalendar(lexicalDate)
The above code is returning null whenever the seconds value is dropped because of 00 seconds value within the parsed string.每当由于解析字符串中的 00 秒值而丢弃秒值时,上面的代码都会返回 null。
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.有人可以协助使用 LocalDate 时间处理此问题的更好方法,否则它可能是 Java 8 LocalDateTime 中的错误/有趣的控件。
Please note I do not have control over this date value, it's coming from a third party platform.请注意,我无法控制此日期值,它来自第三方平台。
You are seeing the documented behavior of the particular DateTimeFormatter
used by the LocalDateTime::toString
method.您正在看到LocalDateTime::toString
方法使用的特定DateTimeFormatter
的记录行为。
Excerpt, my emphasis:摘录,我的重点:
The output will be one of the following ISO-8601 formats:输出将是以下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 .所使用的格式将是输出完整时间值的最短格式,其中省略部分隐含为零。
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 时需要其他行为来表示LocalDateTime
的值,请使用不同的DateTimeFormatter
并将其传递给LocalDateTime::format
。
String output = myLocalDateTime.format( someOtherFormatter ) ;
The LocalDateTime
has no “format” as it is not text. LocalDateTime
没有“格式”,因为它不是文本。 It is the job of the DateTimeFormatter
to parse or generate String
objects of a particular format. DateTimeFormatter
的工作是解析或生成特定格式的String
对象。
您需要使用正确的格式化程序格式化您的日期,而不是通过调用toString()
使用默认的格式化程序。
final String lexicalDate = localDateTime.format(dateTimeFormatter);
您可以使用以下模式来实现所需的格式:
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssz");
DateTimeFormatter removes 00 from date after parsing, you just have to reformat it again DateTimeFormatter 解析后从日期中删除 00,你只需要重新格式化它
please refer below code snippet:请参考以下代码片段:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String currentTime= "2017-10-19 22:00:00";
LocalDateTime datetime = LocalDateTime.parse(currentTime,formatter);
//2017-10-19T22:00
String formattedDate=datetime.format(formatter);
//2017-10-19 22:00:00
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.