java.text.ParseException: Unparseable date: “” (at

2020-06-17 06:03发布

I've read many answers to this problem but no answer solves my problem

I am trying to parse this string :

"2013-10-07T23:21:00+01:00"

to a Date object with the simpledateformat :

"yyyy-MM-dd'T'HH:mm:ssZZZZZ"

but it keeps producing the error:

java.text.ParseException: Unparseable date: "" (at offset 0)

Note: I am trying this on Android, I'm a beginner.

标签: java android
2条回答
疯言疯语
2楼-- · 2020-06-17 06:45

Try with following code

public static Calendar parseDate(String dateTimeStr)
            throws ParseException {
        Calendar calendar = GregorianCalendar.getInstance();
        String s = dateTimeStr.replace("Z", "+00:00");
        try {
            s = s.substring(0, 22) + s.substring(23);
        } catch (IndexOutOfBoundsException e) {
            throw new ParseException("Invalid length", 0);
        }
        Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").parse(s);
        calendar.setTime(date);
        return calendar;
    }
查看更多
Viruses.
3楼-- · 2020-06-17 06:56

If you use java 7, you can use:

yyyy-MM-dd'T'HH:mm:ssXXX

You can check more here

查看更多
登录 后发表回答