Исключение в потоке «main» java.time.format.DateTimeParseException: текст «1 апреля 2022 г., 12:00:00:000AM» не удалось JAVA

Программисты JAVA общаются здесь
Anonymous
Исключение в потоке «main» java.time.format.DateTimeParseException: текст «1 апреля 2022 г., 12:00:00:000AM» не удалось

Сообщение Anonymous »

У меня есть формат даты, например 1 апреля 2022 г., 12:00:00:000, и я хочу преобразовать его в 20220401 (ггггММдд). Как мы можем преобразовать формат датыimport java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Demo {
public static void main(String[] args) {
String value = "Apr 1 2022 12:00:00:000AM";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM d yyyy hh:mm:ss.SSSa");
LocalDateTime dateTime = LocalDateTime.parse(value, formatter);
System.out.println(dateTime);
}
}

При запуске этого кода выдается ошибка ниже
Exception in thread "main" java.time.format.DateTimeParseException: Text 'Apr 1 2022 12:00:00:000AM' could not be parsed at index 4
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.LocalDateTime.parse(LocalDateTime.java:492)
at Demo.main(Demo.java:10)

EDIT-1
Согласно предложениям Василия Бурка, ниже не работает
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Demo {
public static void main(String[] args) {
String value = "Apr 1 2022 12:00:00:000AM".replace( " " , " " ) ;
Locale locale = new Locale.Builder().setLanguage("en").setRegion("US").build();
DateTimeFormatter formatter =
DateTimeFormatter.
ofPattern("MMM d yyyy hh:mm:ss.SSSa")
.withLocale( locale ) ;

LocalDateTime dateTime = LocalDateTime.parse(value, formatter);
System.out.println(dateTime);
}
}

Ошибка все та же
Exception in thread "main" java.time.format.DateTimeParseException: Text 'Apr 1 2022 12:00:00:000AM' could not be parsed at index 19


Подробнее здесь: https://stackoverflow.com/questions/787 ... text-apr-1

Вернуться в «JAVA»