Using DateTimeFormatterBuilder in Java 8, specifically options

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) 
+7
source share
2 answers

This may be the reason that +0000 not a zone identifier, but a zone offset.

The documentation offers this list:

  Symbol Meaning Presentation Examples ------ ------- ------------ ------- V time-zone ID zone-id America/Los_Angeles; Z; -08:30 z time-zone name zone-name Pacific Standard Time; PST O localized zone-offset offset-O GMT+8; GMT+08:00; UTC-08:00; X zone-offset 'Z' for zero offset-X Z; -08; -0830; -08:30; -083015; -08:30:15; x zone-offset offset-x +0000; -08; -0830; -08:30; -083015; -08:30:15; Z zone-offset offset-Z +0000; -0800; -08:00; 

You can use appendOffset("+HHMM", "0000") ( doc ) or appendZoneOrOffsetId() ( doc ) instead of appendZoneId() .

so your full formatter might look like this

 DateTimeFormatter formatter = new DateTimeFormatterBuilder() .parseCaseInsensitive() .append(DateTimeFormatter.ISO_LOCAL_DATE_TIME) .optionalStart() .appendPattern(".SSS") .optionalEnd() .optionalStart() .appendZoneOrOffsetId() .optionalEnd() .optionalStart() .appendOffset("+HHMM", "0000") .optionalEnd() .toFormatter(); 

Further, the method of creating ZonedDateTime may affect if there is an exception or not. Therefore, I would recommend the following, as this worked without any exceptions.

 LocalDateTime time = LocalDateTime.parse("2013-09-20T07:00:33.123+0000", formatter); ZonedDateTime zonedTime = time.atZone(ZoneId.systemDefault()); 
+12
source

Have you tried .appendPattern("ZZZ") ? it will probably work!

0
source

Source: https://habr.com/ru/post/985388/


All Articles