Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

DateTimeFormatter fails to parse a date in JDK 17 where as passes in JDK8

Here is the code snippet

String date = "Wed, 20 Feb 2019 07:14:06 +0100";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE, d MMM yyyy HH:mm:ss Z");
System.out.println(ZonedDateTime.parse(date, formatter).toString());

This code works fine with JDK8 where as fails in JDK17 with the following exception

Text 'Wed, 20 Feb 2019 07:14:06 +0100' could not be parsed at index 0
java.time.format.DateTimeParseException: Text 'Wed, 20 Feb 2019 07:14:06 +0100' could not be parsed at index 0
    at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2052)
    at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1954)
    at java.base/java.time.ZonedDateTime.parse(ZonedDateTime.java:600)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your problem has nothing to do with Java 8 versus Java 17.

Tip: Before blaming software that is formally specified, is thoroughly tested by enormous test suites, and is used by millions of programmers daily, suspect your own code first.

Locale

Specify a Locale. The locale determines the human language and cultural norms used in translating month name, etc.

Locale locale = Locale.US ;
DateTimeFormatter formatter = 
    DateTimeFormatter
    .ofPattern( "EEE, d MMM yyyy HH:mm:ss Z" )
    .withLocale( locale );
    
String input = "Wed, 20 Feb 2019 07:14:06 +0100" ;
ZonedDateTime zdt = ZonedDateTime.parse( input , formatter ) ;
String output = zdt.toString() ;
System.out.println( output );

See this code run at Ideone.com.

2019-02-20T07:14:06+01:00


Tip: Educate the publisher of your data about the virtues in using only ISO 8601 standard formats for communicating date-time values textually.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading