-2

I can't seem to come up with a pattern for "March 26 2020" (no comma). Can someone help? I've tried DateTimeFormatter.ofPattern("MMMM dd yyyy") and DateTimeFormatter.ofPattern("MMM dd yyyy"). Any help would be appreciated!

Minimal reproducible example:

    String strDate = "March 26 2020";
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MMMM dd uuuu");
    LocalDate date = LocalDate.parse(strDate, dtf);
    System.out.println(date);

On my computer this throws

Exception in thread "main" java.time.format.DateTimeParseException: Text 'March 26 2020' could not be parsed at index 0

Expected output was

2020-03-26

With MMM in the format pattern instead the exception is the same.

Braiam
  • 1
  • 11
  • 50
  • 74
CNDyson
  • 1,798
  • 6
  • 24
  • 52

2 Answers2

5

Use the pattern, MMMM dd uuuu with Locale.ENGLISH.

Demo:

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

class Main {
    public static void main(String[] args) {
        String strDate = "March 26 2020";
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MMMM dd uuuu", Locale.ENGLISH);
        LocalDate date = LocalDate.parse(strDate, dtf);
        System.out.println(date);
    }
}

Output:

2020-03-26
Arvind Kumar Avinash
  • 62,771
  • 5
  • 54
  • 92
0

Try this

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM' 'dd' 'yyyy", Locale.ENGLISH)

Worked for me just fine, Output:

December 10 2020
Michael Gantman
  • 5,975
  • 1
  • 17
  • 34