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
java.text.ParseException: Unparseable date: "" (at offset 0)
提问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
回答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;
}