1

我有几行代码,如下所示

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

这给出了以下例外

 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)

如果我将月份从11月更改为11 月,它工作正常。我从数据库表中得到这个值。我必须以编程方式更改它还是有什么方法可以处理它?

4

1 回答 1

3

您可以配置您DateTimeFormatter的忽略案例

DateTimeFormatter formatter = new DateTimeFormatterBuilder()
                .parseCaseInsensitive()
                .appendPattern("dd-MMM-yy")
                .toFormatter(Locale.getDefault());
于 2021-07-13T14:21:22.347 回答