java.text.ParseException: Unparseable date: “” (at offset 0) [duplicate]

一笑奈何 提交于 2019-12-05 00:19:06

问题


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.


回答1:


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;
    }



回答2:


If you use java 7, you can use:

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

You can check more here



来源:https://stackoverflow.com/questions/19246936/java-text-parseexception-unparseable-date-at-offset-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!