Is there a way how to create JSR-310 formatter that is able to parse both following date/times with variable length of seconds fraction?
2015-05-07 13:20:22.276052
or
2015-05-07 13:20:22.276
Example code:
DateTimeFormatter formatter
= new java.time.format.DateTimeFormatterBuilder()
.append( java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") )
.appendOptional( java.time.format.DateTimeFormatter.ofPattern(".SSSSSS") )
.toFormatter();
formatter.parse("2015-05-07 13:20:22.276052", LocalDateTime::from);
LocalDateTime.parse
rather than using the formatter directly. Yet the doc forDateTimeFormatter.parse
says just the opposite: “Most applications should use this method for parsing.” …LocalDateTime dt = parser.parse(str, LocalDateTime::from);
– Thermomagnetic