ParseException when parsing 3 character abbreviated month using SimpleDateFormat

前端 未结 5 502
说谎
说谎 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:21
    Date dft  = (Date) format.parse("16-05-2018 09:30:22");
    

    OR change it to

    SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2020-12-22 15:31

    you are using dd-MM-yyyy HH:mm:ss, so parsable is

    16-05-2018 09:30:22
    

    and if you want 16-MAY-2018 09:30:22 then use

    dd-MMM-yyyy HH:mm:ss
    
    0 讨论(0)
  • 2020-12-22 15:42

    Use this pattern: dd-MMM-yyyy HH:mm:ss

    0 讨论(0)
  • 2020-12-22 15:44

    "16-MAY-2018 09:30:22" is not parsable with that time format. If you want to parse that you have to change date format to "dd-MMM-yyyy HH:mm:ss". The double M is only for numbered months (so should be 05 for May).

    Check the SimpleDateFormat javadoc for more details: here

    0 讨论(0)
提交回复
热议问题