Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: DateTimeFormatter fail to parse time string when seconds and milliseconds are all 0s?

Basically, I am using the following code to parse string as LocalDateTime, which works fine most of the time.

DateTimeFormatter dtformatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");

However, I encounter cases where the seconds and millseconds are 00000 and this is when the parser fails and print a LocalDateTime 2018-03-01T09:16 instead of 2018-03-01T09:16:00.000.

System.out.println(LocalDateTime.parse("20180301091600000",dtformatter));

(Note that in my code, I have to parse string as LocalDateTime, do some comparison and then at the end, print LocalDateTime to csv)

How can I fix it to make it print 2018-03-01T09:16:00.000 instead of 2018-03-01T09:16 ?

FYI, I am using jdk10.

like image 730
mynameisJEFF Avatar asked Apr 22 '18 14:04

mynameisJEFF


People also ask

Is Java Time format DateTimeFormatter thread-safe?

Yes, it is: DateTimeFormat is thread-safe and immutable, and the formatters it returns are as well.

What is the difference between DateTimeFormatter and SimpleDateFormat?

DateTimeFormatter is a replacement for the old SimpleDateFormat that is thread-safe and provides additional functionality.

How do I use DateTimeFormatter with date?

DateTimeFormatter fmt = DateTimeFormatter. ofPattern("yyyy-MM-dd'T'HH:mm:ss"); System. out. println(ldt.

What is the role of the DateTimeFormatter type in Java?

The DateTimeFormatter class is used to both parse and format dates according to specified Date and Time Patterns. Use parse(...) method to convert from String to Date/Time classes, use format(...) method to convert from Date/Time into String.


1 Answers

tl;dr

where the seconds and millseconds are 00000 and this is when the parser fails

No, the parser succeeds. Your issue is with generating a String, not parsing.

The default DateTimeFormatter suppresses zero values in seconds and fractional second, as documented.

Feature, not a bug

Your problem is not in the parsing, but in the generating of a string after parsing. Keep in mind that the textual representation of a date-time object is distinct and separate from the object. In other words, a date-time object does not have a “format”.

[String] --> parse --> [LocalDateTime] --> toString --> [String]

The documentation for LocalDateTime::toString clearly says that the shortest possible formatting variation will be used when encountering zero values in the least-significant parts. To quote:

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.

Examples

Regarding two examples shown in the accepted Answer by YCF_L…

20180301091600001 result is 2018-03-01T09:16:00.001

In that example, the least-significant part (millisecond) has a non-zero value, so it is represented in the result.

2018030100000000 result is 2018-03-01T00:00

In that example, the least significant parts of hour, minute, second, milliseconds, microseconds, and nanoseconds are all zero. So their display is suppressed, except for hours and minutes as the documentation promises that year-minute is always displayed.

So both of your examples work as documented; feature, not a bug.

Solution

The solution is to not use the default formatter provided in the toString method. Instead, use another formatter. For example, use the same custom formatter you defined for parsing.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "yyyyMMddHHmmssSSS" );
LocalDateTime ldt = LocalDateTime.parse( "20180301091600000" , f );

String outputDefault = ldt.toString();
String outputCustom = ldt.format( f );

Dump to console.

System.out.println( "outputDefault: " + outputDefault );
System.out.println( "outputCustom: " + outputCustom );

outputDefault: 2018-03-01T09:16

outputCustom: 20180301091600000

The Question asks:

How can I fix it to make it print 2018-03-01T09:16:00.000 instead of 2018-03-01T09:16 ?

Specify a custom formatter instead of the default formatter.

DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss:SSS") ; 
String output = ldt.format( f ) ;

But keep in mind your generated String will suppress display of any microseconds or nanoseconds in the LocalDateTime object.

like image 50
Basil Bourque Avatar answered Sep 22 '22 23:09

Basil Bourque