DateTimeFormatter format with a single pattern letter fails to parse short month name

I see the below error when I run the code listed below :

Exception in thread "main" java.time.format.DateTimeParseException: Text 'Thu Sep 21 23:47:03 EDT 2017' could not be parsed at index 4 at java.time.format.DateTimeFormatter.parseResolved0(Unknown Source) at java.time.format.DateTimeFormatter.parse(Unknown Source) at java.time.LocalDate.parse(Unknown Source) at com.example.demo.DateTest.main(DateTest.java:16)

Code

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class DateTest {    
    public static void main(String[] args) {
        String date = "Thu Sep 21 23:47:03 EDT 2017";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E M d H:m:s z y", Locale.ENGLISH);
        LocalDate startDate = LocalDate.parse(date, formatter);
        System.out.println(date);           
    }    
}
Jon Skeet
people
quotationmark

You're using M as the month format specifier, but M is a number/text field, so it's expecting a numeric value.

I also suspect you mean HH:mm:ss rather than H:m:s for the time part, and yyyy for the year. (The latter is unlikely to be a problem unless you actually have dates before 1000AD.)

This works:

DateTimeFormatter formatter =
    DateTimeFormatter.ofPattern("E MMM d HH:mm:ss z yyyy", Locale.ENGLISH);

people

See more on this question at Stackoverflow