如果月份使用大写或小写字母而不是标题大小写,DateTimeFormatter无法解析日期。是否有一种简单的方法将日期转换为标题大小写,或者有一种方法可以使格式化程序更宽容?
for (String date : "15-JAN-12, 15-Jan-12, 15-jan-12, 15-01-12".split(", ")) {
try {
System.out.println(date + " => " + LocalDate.parse(date,
DateTimeFormatter.ofPattern("yy-MMM-dd")));
} catch (Exception e) {
System.out.println(date + " => " + e);
}
}
打印
15-JAN-12 => java.time.format.DateTimeParseException: Text '15-JAN-12' could not be parsed at index 3
15-Jan-12 => 2015-01-12
15-01-12 => java.time.format.DateTimeParseException: Text '15-01-12' could not be parsed at index 3
15-jan-12 => java.time.format.DateTimeParseException: Text '15-jan-12' could not be parsed at index 3
.parseLenient()
,格式为“15-01-12”的日期将无法解析。 - SubOptimal