Parsing SimpleDateFormat

后端 未结 2 562
轻奢々
轻奢々 2020-12-22 14:23

I have this date that I seem to be unable to parse correctly.

String text \"Wed May 21 05:44:09 -0700 2014\";

This is my date format

public s         


        
2条回答
  •  粉色の甜心
    2020-12-22 14:46

    To parse your date you can use

    SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_PATTERN);
    Date parsedDate = sdf.parse("Wed May 21 05:44:09 -0700 2014");
    

    But if that fails and you are seeing

    java.text.ParseException: Unparseable date: "Wed May 21 05:44:09 -0700 2014"

    then most probably Wed is not recognised by your default locale as correct day. In that case you will have to set locale to place where this word is recognized, like

    SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_PATTERN, Locale.US);
    //                                                               ^^^^^^^^^
    

提交回复
热议问题