Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

datetime - 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:

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

instead of :

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)

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

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 third party platform.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Feature, not a bug

You are seeing the documented behavior of the particular DateTimeFormatter used by the LocalDateTime::toString method.

Excerpt, my emphasis:

The output will be one of the following ISO-8601 formats:

uuuu-MM-dd'T'HH:mm

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

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

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

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 output = myLocalDateTime.format( someOtherFormatter ) ;

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...