Why does the following fail to run, with the date-time string unable to parse as an OffsetDateTime
?
String inputOdt = "2016-01-23T12:34:56 GMT+8";
DateTimeFormatter formatterOdt = DateTimeFormatter.ofPattern ( "yyyy-MM-dd'T'HH:mm:ss O" );
OffsetDateTime odt = OffsetDateTime.parse ( inputOdt , formatterOdt );
Using Java(TM) SE Runtime Environment (build 1.8.0_92-b14)
on Mac OS X El Capitan 10.11.4.
Generates error:
Exception in thread "main" java.time.format.DateTimeParseException: Text '2016-01-23T12:34:56 GMT+8' could not be parsed: String index out of range: 25
The offset-from-UTC string GMT+8
is copied-pasted from the example in the class documentation for DateTimeFormatter
. To quote:
Offset O: This formats the localized offset based on the number of pattern letters. One letter outputs the short form of the localized offset, which is localized offset text, such as 'GMT', with hour without leading zero, optional 2-digit minute and second if non-zero, and colon, for example 'GMT+8'.
The rest of the string parses successfully as a LocalDateTime
. So the problem seems to indeed be the offset-from-UTC part.
String inputLdt = "2016-01-23T12:34:56";
DateTimeFormatter formatterLdt = DateTimeFormatter.ofPattern ( "yyyy-MM-dd'T'HH:mm:ss" );
LocalDateTime ldt = LocalDateTime.parse ( inputLdt , formatterLdt );
System.out.println ( "" );
System.out.println ( "inputLdt: " + inputLdt );
System.out.println ( "ldt: " + ldt );
inputLdt: 2016-01-23T12:34:56
ldt: 2016-01-23T12:34:56
Workaround
A partial workaround is to add a trailing SPACE to both the input string and the formatting pattern. So this works.
String input = "Sat May 02 2015 00:00:00 GMT+08 "; // Trailing space.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern ( "EEE MMM dd yyyy HH:mm:ss O " ); // Trailing space.
OffsetDateTime odt = OffsetDateTime.parse ( input , formatter ); // SUCCEEDS
But adding minutes without a colon is documented as working with a single O
but it fails. This workaround of a trailing SPACE does not help in such a case. Notice the GMT+0800
in this example versus GMT+08
seen directly above, where this example fails but the one above succeeds.
String input = "Sat May 02 2015 00:00:00 GMT+0800 "; // Minutes in the offset, and trailing space.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern ( "EEE MMM dd yyyy HH:mm:ss O " ); // Trailing space.
OffsetDateTime odt = OffsetDateTime.parse ( input , formatter ); // FAILS
StringIndexOutOfBoundsException
makes me think this is a bug. – Meprobamate+08
) but not for hour-and-minute offset (+0800
). I edited my Answer to show my experiments using Java 8 Update 92. – Steinberg