在解析具有大写月份名称的日期时,我得到 java.time.format.DateTimeParseException

JAV*_*CAT 4 java-8 java-time datetimeformatter

我有几行代码,如下所示

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MMM-yy");
formatter = formatter.withLocale( Locale.getDefault() );  
LocalDate date = LocalDate.parse("11-NOV-20", formatter);
Run Code Online (Sandbox Code Playgroud)

这给出了以下异常

 Exception in thread "main" java.time.format.DateTimeParseException: Text '11-NOV-20' could not be parsed at index 3
        at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
        at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
        at java.time.LocalDate.parse(LocalDate.java:400)
        at src.DateTimeTest.main(DateTimeTest.java:30)
Run Code Online (Sandbox Code Playgroud)

如果我将月份从11 月更改为11 月,效果很好。我从数据库表中获取这个值。我是否必须以编程方式更改它,或者有什么方法可以处理它?

div*_*poc 7

您可以配置DateTimeFormatter忽略大小写

DateTimeFormatter formatter = new DateTimeFormatterBuilder()
                .parseCaseInsensitive()
                .appendPattern("dd-MMM-yy")
                .toFormatter(Locale.getDefault());
Run Code Online (Sandbox Code Playgroud)