I'm trying to create a DateTimeFormatter
to match the following example (it's actually slightly more complex than this but that shouldn't matter).
20180302-17:45:21
I've written the following but it results in an exception:
new DateTimeFormatterBuilder()
.append(DateTimeFormatter.BASIC_ISO_DATE)
.appendLiteral('-')
.append(DateTimeFormatter.ISO_LOCAL_TIME)
.toFormatter()
.parse("20180302-17:45:21");
The exception is:
Exception in thread "main" java.time.format.DateTimeParseException: Text '20180302-17:45:21' could not be parsed at index 11
at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1988)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1816)
It appears to be failing on the colon between 17:45
and DateTimeFormatterBuilder.appendLiteral
doesn't give any clues.
If I change the literal to another character, let's say m
, then it works fine:
new DateTimeFormatterBuilder()
.append(DateTimeFormatter.BASIC_ISO_DATE)
.appendLiteral('m')
.append(DateTimeFormatter.ISO_LOCAL_TIME)
.toFormatter()
.parse("20180302m17:45:21");
What's going on here? How can I fix it, assuming I can't change the format?
Comments suggest this might be version dependent. I'm using JDK 9.0.1 and it's been reproduced on 9.0.4.