如何处理 JSR 310 中的大写或小写?
如果月份是大写或小写,即不是标题大小写,则 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