If a month is in UPPER or lower case, i.e. not Title case, DateTimeFormatter cannot parse the date. Is there a simple way to convert a date to title case, or a way to make the formatter more lenient?
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);
}
}
prints
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