I am trying to switch from Joda to Java 8 ZonedDateTime and I click on the wall with a DateTimeFormatterBuilder that I cannot work with.
I want to accept any of these formats:
2013-09-20T07:00:33 2013-09-20T07:00:33.123 2013-09-20T07:00:33.123+0000 2013-09-20T07:00:33.123Z 2013-09-20T07:00:33.123Z+0000 2013-09-20T07:00:33+0000
Here is my current builder:
DateTimeFormatter formatter = new DateTimeFormatterBuilder() .parseCaseInsensitive() .append(DateTimeFormatter.ISO_LOCAL_DATE_TIME) .optionalStart() .appendPattern(".SSS") .optionalEnd() .optionalStart() .appendZoneId() .optionalEnd() .optionalStart() .appendPattern("Z") .optionalEnd() .toFormatter();
I'm probably mistaken, but it looks like this should fit the patterns I want ... right?
If anyone could indicate that I might have missed, that would be appreciated. I'm also not too sure about using appendOffset , so clarity is also appreciated if it turns out to be the answer.
Edit:
Text '2013-09-20T07:00:33.061+0000' could not be parsed at index 23
Looking at the builder, does this seem to be related to optional steps?
Edit 2:
Seeing the advice from the first answer, I tried this:
DateTimeFormatter formatter = new DateTimeFormatterBuilder() .parseCaseInsensitive() .append(DateTimeFormatter.ISO_LOCAL_DATE_TIME) .optionalStart() .appendPattern(".SSS") .optionalEnd() .optionalStart() .appendZoneOrOffsetId() .optionalEnd() .toFormatter()
He continues to fail in the line above.
Edit 3:
Recent tests lead to this exception:
java.time.format.DateTimeParseException: Text '2013-09-20T07:00:33.061+0000' could not be parsed at index 23 at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1947) at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1849) at java.time.ZonedDateTime.parse(ZonedDateTime.java:597) at java.time.ZonedDateTime.parse(ZonedDateTime.java:582)