java.text.ParseException:无法解析的日期:“”(偏移量为 0)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19246936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 15:25:46  来源:igfitidea点击:

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

javaandroid

提问by theanimashaun

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"

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

to a Date object with the simpledateformat :

到具有 simpledateformat 的 Date 对象:

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

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

but it keeps producing the error:

但它不断产生错误:

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

java.text.ParseException:无法解析的日期:“”(偏移量为 0)

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

注意:我正在 Android 上尝试此操作,我是初学者。

回答by BobTheBuilder

If you use java 7, you can use:

如果您使用 java 7,则可以使用:

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

You can check more here

你可以在这里查看更多

回答by Sunil Kumar Sahoo

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