[Solved] java.text.ParseException: Unparseable date: “Augu 16, 1979” [duplicate]


The problem is that replace("st", "") also removes the st ending of August, resulting in the input string seen in the error message.

To handle this, you need to make sure the st suffix is right after a digit, so it’s part of the day value. You also need to handle all of 1st, 2nd, 3rd, and 4th.

This means that you should use regular expression, like this:

replaceFirst("(?<=\\d)(?:st|nd|rd|th)", "")

Test

public static void main(String[] args) throws Exception {
    test("August 20th, 2012");
    test("August 21st, 2012");
    test("August 22nd, 2012");
    test("August 23rd, 2012");
    test("August 24th, 2012");
}
static void test(String input) throws ParseException {
    String modified = input.replaceFirst("(?<=\\d)(?:st|nd|rd|th)", "");

    DateFormat originalFormat = new SimpleDateFormat("MMMM dd, yyyy", Locale.ENGLISH);
    DateFormat targetFormat = new SimpleDateFormat("yyyyMMdd");
    Date date = originalFormat.parse(modified);
    System.out.println(targetFormat.format(date));
}

Output

20120820
20120821
20120822
20120823
20120824

1

solved java.text.ParseException: Unparseable date: “Augu 16, 1979” [duplicate]