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

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

    The following code works. The problem is you are using "JAN" instead of "Jan". DateTimeFormatter does not recognize that it seems. and also change the pattern to "d-MMM-yyyy".

      String date1 ="01-Jan-2017";
      String date2 = "02-Feb-2017";
    
      DateTimeFormatter df = DateTimeFormatter.ofPattern("d-MMM-yyyy");
      LocalDate  d1 = LocalDate.parse(date1, df);
      LocalDate  d2 = LocalDate.parse(date2, df);
    
      Long datediff = ChronoUnit.DAYS.between(d1,d2);  
    

    Source: https://www.mkyong.com/java8/java-8-how-to-convert-string-to-localdate/

提交回复
热议问题