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

后端 未结 5 1456
醉酒成梦
醉酒成梦 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:41

    Maybe you can use this wildcard,

     String d2arr[] = {
                "2016-12-21",
                "1/17/2016",
                "1/3/2016",
                "11/23/2016",
                "OCT 20 2016",
                "Oct 22 2016",
                "Oct 23", // default year is 2016
                "OCT 24",  // default year is 2016
        };
    
        DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder()
                .parseCaseInsensitive().parseLenient()
                .parseDefaulting(ChronoField.YEAR_OF_ERA, 2016L)
                .appendPattern("[yyyy-MM-dd]")
                .appendPattern("[M/dd/yyyy]")
                .appendPattern("[M/d/yyyy]")
                .appendPattern("[MM/dd/yyyy]")
                .appendPattern("[MMM dd yyyy]");
    
        DateTimeFormatter formatter2 = builder.toFormatter(Locale.ENGLISH);
    

    https://coderanch.com/t/677142/java/DateTimeParseException-Text-parsed-unparsed-textenter link description here

提交回复
热议问题