简体   繁体   中英

Trying to convert string date into a date

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.Date prevStartDate = new Date();
java.util.Date prevStopDate = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
prevStartDate = dateFormat.format(startDateLY);

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));
}
DateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy");
Date date = (Date)formatter.parse("Sat May 01 00:00:00 EDT 2010");
String string = new String(date.getYear() + "-" + date.getMonth() + "-" + date.getDay());

Should work better then just yyyy-MM-dd .

SimpleDateFormat is Locale dependent, so by providing one you can get the Date string localized for specific language or country

http://www.javablogging.com/java-simpledateformat-examples/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM