首页 文章

java.text.ParseException:无法解析的日期:“”(在偏移0处)[重复]

提问于
浏览
6

这个问题在这里已有答案:

我已经阅读了很多这个问题的答案,但没有答案解决了我的问题

我试图解析这个字符串:

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

使用simpledateformat到Date对象:

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

但它不断产生错误:

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

注意:我在Android上尝试这个,我是初学者 .

2 回答

  • 2

    尝试使用以下代码

    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

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

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

    你可以查看更多here

相关问题