MauriceNino
MauriceNino

Reputation: 6757

DateTimeParseException on 'z' (time-zone name) when using DateTimeFormatter

Using OpenJDK 11.0.3, when I want to format a date with a time-zone, I get the following error:

String pattern = "EEE MMM dd HH:mm:ss z yyyy";
String dateStr = "Thu Jan 01 01:00:00 CET 1970";

DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern, Locale.ENGLISH);

LocalDate date = LocalDate.parse(dateStr, formatter);
java.time.format.DateTimeParseException: Text 'Thu Jan 01 01:00:00 CET 1970' could not be parsed: null
    at java.base/java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:2017)
    at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1952)
    at java.base/java.time.LocalDate.parse(LocalDate.java:428)
...
Caused by: java.lang.NullPointerException
    at java.base/java.time.format.DateTimeFormatterBuilder$PrefixTree.prefixLength(DateTimeFormatterBuilder.java:4527)
    at java.base/java.time.format.DateTimeFormatterBuilder$PrefixTree.add0(DateTimeFormatterBuilder.java:4396)
    at java.base/java.time.format.DateTimeFormatterBuilder$PrefixTree.add(DateTimeFormatterBuilder.java:4391)
...

When I omit the time-zone name (z), everything works just fine:

String pattern = "EEE MMM dd HH:mm:ss yyyy";
String dateStr = "Thu Jan 01 01:00:00 1970";

DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern, Locale.ENGLISH);

LocalDate date = LocalDate.parse(dateStr, formatter);
date ==> 1970-01-01

Upvotes: 1

Views: 286

Answers (1)

MauriceNino
MauriceNino

Reputation: 6757

There seems to be a bug with this version.

When using Version 11.0.11 of the OpenJDK, everything works like it should:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern(
   "EEE MMM dd HH:mm:ss z yyyy", 
   Locale.ENGLISH
);
LocalDate date = LocalDate.parse("Thu Jan 01 01:00:00 CET 1970", formatter);
date ==> 1970-01-01

Unfortunately, I can't find anything related to this issue on the internet, so I can't really tell why that happened in the older version.

Upvotes: 1

Related Questions