在用大写月份名称解析日期时,我得到 java.time.format.DateTimeParseException
While parsing date with upper case month name I get java.time.format.DateTimeParseException
我有几行代码如下所示
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)
如果我将月份从 NOV 更改为 Nov,它可以正常工作。我从数据库 table 中获取此值。我必须以编程方式更改它还是有任何方法可以处理它?
您可以将 DateTimeFormatter
配置为忽略大小写
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.appendPattern("dd-MMM-yy")
.toFormatter(Locale.getDefault());
我有几行代码如下所示
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)
如果我将月份从 NOV 更改为 Nov,它可以正常工作。我从数据库 table 中获取此值。我必须以编程方式更改它还是有任何方法可以处理它?
您可以将 DateTimeFormatter
配置为忽略大小写
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.appendPattern("dd-MMM-yy")
.toFormatter(Locale.getDefault());