ParseException when parsing 3 character abbreviated month using SimpleDateFormat

前端 未结 5 532
说谎
说谎 2020-12-22 14:56

Here is my code,

SimpleDateFormat format = new SimpleDateFormat(\"dd-MM-yyyy HH:mm:ss\");
Date dft  = (Date) format.parse(\"16-MAY-2018 09:30:22:000\");
         


        
5条回答
  •  一个人的身影
    2020-12-22 15:29

    The pattern should be MMM because there are three characters in the month.

    You should also prefer java.time classes to the ones you're currently using if you're on Java 8 or above:

    DateTimeFormatter formatter = new DateTimeFormatterBuilder()
        .appendPattern("dd-MMM-yyyy")
        .appendLiteral(' ')
        .append(ISO_LOCAL_TIME)
        .toFormatter();
    
    LocalDateTime timestamp = LocalDateTime.parse("16-May-2018 09:30:22", formatter);
    

提交回复
热议问题