I have input strings in nearly standard ISO 8601, but the minute-of-hour part is omitted (don't ask why, funky data feed out of my control).
2018-01-23T12
How do I parse that while letting the minute-of-hour default to zero?
2018-01-23T12:00
DateTimeFormatter.ISO_LOCAL_DATE_TIME
The default parser for LocalDateTime is DateTimeFormatter.ISO_LOCAL_DATE_TIME. This formatter tolerates optional second-of-minute. The fractional second and the second-of-minute are both set to zero.
LocalDateTime ldt = LocalDateTime.parse( "2018-01-23T12:00" ) ; // Works.
But omitting the minute-of-hour fails, with exception thrown.
LocalDateTime ldt = LocalDateTime.parse( "2018-01-23T12" ) ; // Fails.
DateTimeFormatterBuilder
I am aware of the DateTimeFormatterBuilder class and its ability to tolerate optional parts while setting default values.
But I have been able to use it properly. I tried the pattern "uuuu-MM-dd HH" while setting both minute-of-hour and second-of-minute to default to zero.
String input = "2018-01-23T12";
DateTimeFormatterBuilder b = new DateTimeFormatterBuilder().parseDefaulting( ChronoField.MINUTE_OF_HOUR , 0 ).parseDefaulting( ChronoField.SECOND_OF_MINUTE , 0 ).appendPattern( "uuuu-MM-dd HH" );
DateTimeFormatter f = b.toFormatter( Locale.US );
LocalDateTime ldt = LocalDateTime.parse( input , f );
System.out.println( ldt );
Exception thrown:
Exception in thread "main" java.time.format.DateTimeParseException: Text '2018-01-23T12' could not be parsed at index 10
at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1988)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1890)
at java.base/java.time.LocalDateTime.parse(LocalDateTime.java:492)
at com.basilbourque.example.App.doIt(App.java:31)
at com.basilbourque.example.App.main(App.java:22)
+":00"to the end of the strings?DateTimeFormatterBuilderworks. In other words, I'm more interested in an education than a solution. This particular problem seemed like a good example for a Stack Overflow Question, as none of the other six Questions onDateTimeFormatterBuilderwith "optional" or "default" cover this.