DateTimeFormatter has built-in formats that can be used directly to analyze character sequences. This is case sensitive, November will work, however, new and NOV will not work:
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MMM-dd"); try { LocalDate datetime = LocalDate.parse(oldDate, pattern); System.out.println(datetime); } catch (DateTimeParseException e) { // DateTimeParseException - Text '2019-nov-12' could not be parsed at index 5 // Exception handling message/mechanism/logging as per company standard }
DateTimeFormatterBuilder provides its own way to create a formatter. This is not case sensitive, November, November and November will be considered the same.
DateTimeFormatter f = new DateTimeFormatterBuilder().parseCaseInsensitive() .append(DateTimeFormatter.ofPattern("yyyy-MMM-dd")).toFormatter(); try { LocalDate datetime = LocalDate.parse(oldDate, f); System.out.println(datetime); // 2019-11-12 } catch (DateTimeParseException e) { // Exception handling message/mechanism/logging as per company standard }
Prashant singh Chouhan Aug 26 '19 at 9:05 a.m. 2019-08-26 09:05
source share