java.time.format.DateTimeParseException: Text could not be parsed at index 3

后端 未结 5 1443
醉酒成梦
醉酒成梦 2021-02-06 23:36

I am using Java 8 to parse the the date and find difference between two dates.

Here is my snippet:

String date1 =\"01-JAN-2017\";
String date2 = \"02-FEB         


        
5条回答
  •  孤城傲影
    2021-02-07 00:29

    // DateTimeFormatterBuilder provides custom way to create a
        // formatter
        // It is Case Insensitive, Nov , nov and NOV will be treated same
        DateTimeFormatter f = new DateTimeFormatterBuilder().parseCaseInsensitive()
                .append(DateTimeFormatter.ofPattern("yyyy-MMM-dd")).toFormatter();
        try {
            LocalDate datetime = LocalDate.parse("2019-DeC-22", f);
            System.out.println(datetime); // 2019-12-22
        } catch (DateTimeParseException e) {
            // Exception handling message/mechanism/logging as per company standard
        }
    

提交回复
热议问题