The following testcase runs perfectly under Java 10:
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
class Test
{
public static void main (String[] args) throws java.lang.Exception
{
DateTimeFormatter dateFormatter = new DateTimeFormatterBuilder().
appendPattern("EEE, dd MMM yyyy HH:mm:ss zzz").
toFormatter();
Instant result = dateFormatter.parse("Sat, 29 Sep 2018 20:49:02 GMT", Instant::from);
System.out.println("Result: " + result);
}
}
but under Java 11 I get:
Exception in thread "main" java.time.format.DateTimeParseException: Text 'Sat, 29 Sep 2018 20:49:02 GMT' could not be parsed at index 0
at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2046)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
at Test.main(Test.java:13)
What's going on?
UPDATE: Replacing toFormatter()
with toFormatter(Locale.US)
fixes the problem. I am guessing this issue is related to https://bugs.openjdk.java.net/browse/JDK-8206980. This issue is marked as fixed in Java 11 build 23 but I am running
openjdk version "11" 2018-09-25
OpenJDK Runtime Environment 18.9 (build 11+28)
OpenJDK 64-Bit Server VM 18.9 (build 11+28, mixed mode)
Shouldn't this be fixed in this version?
UPDATE2: If you are unable to reproduce the problem, try replacing toFormatter()
with toFormatter(Locale.CANADA)
.