Trying to convert string date into a date

前端 未结 3 1956
有刺的猬
有刺的猬 2021-01-28 17:42

java.text.ParseException: Unparseable date: \"Sat May 01 00:00:00 EDT 2010\"

I am trying to parse this date using the SimpleDateFormat class.

java.util.         


        
3条回答
  •  感情败类
    2021-01-28 18:21

    That would be because you're using the format of yyyy-MM-dd - you have to add each parameter in your input to that format.

    It looks like your format is E MMM dd HH:mm:ss z yyyy

    So you need to convert from one to the other:

    static DateFormat extended = new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy");
    static DateFormat simple = new SimpleDateFormat("yyyy-MM-dd");
    
    String reformat(String extendedString) {
        Date yourDate = extended.parse(extendedString);
        String simpleString = simple.format(yourDate);
        return simpleString;
    }
    

    Or alternatively,

    String reformat(String dateString) {
        return simple.format(extended.parse(dateString));
    }
    

提交回复
热议问题