简体   繁体   中英

Java parse a 8601 formatted string to a more readable date parse error

I'm getting dates and trying to parse them into more readable dates but I keep getting a parse error, the formatting looks correct to me but its not working

 DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'");
 DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");
    try {
        Date date1 = df.parse(purchase.getRedeem_at());

        String readableDate = df2.format(date1);

        b.append(readableDate);

    } catch (ParseException e) {

        e.printStackTrace();
    }

An example string im trying to parse is

2014-08-12T02:30:00Z

You're missing the seconds part from your first format string.

DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
//                                                       ^

joda will parse this format right out of the box:

DateTimeFormatter fmt = ISODateTimeFormat.dateTimeNoMillis();
DateTime ourDate = fmt.parseDateTime(jdkDate);

If you have further problems, do leave a comment.

您应该添加“ ss”

DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); 

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