[英]Unparseable date: “Fri Oct 10 23:11:07 IST 2014” (at offset 20)
I have created this funtion to parse date but this gives exception : Unparseable date: "Fri Oct 10 23:11:07 IST 2014" (at offset 20) . 我已经创建了这个函数来解析日期,但这给出了异常:Unparseable date:“Fri Oct 10 23:11:07 IST 2014”(在偏移20处)。 Please help as I am not able to figure out whats wrong with this code. 请帮忙,因为我无法弄清楚这段代码有什么问题。
public Date parseDate() {
String strDate ="Fri Oct 10 23:11:29 IST 2014";
String newPattern = "EEE MMM dd HH:mm:ss Z yyyy";
SimpleDateFormat formatter = new SimpleDateFormat(newPattern);
try {
Date date = formatter.parse(strDate);
return date;
} catch (java.text.ParseException e) {
e.printStackTrace();
}
return null;
}
Use a locale for the parser: 使用解析器的区域设置:
SimpleDateFormat formatter = new SimpleDateFormat(newPattern, Locale.US);
This should fix your problem. 这应该可以解决您的问题。 At least it works for me with your example. 至少它适用于我的例子。
EDIT: 编辑:
It looks like there is indeed a problem with Android and IST timezone. 看起来Android和IST时区确实存在问题。 I can parse any time zone on Android using the above pattern, but not IST. 我可以使用上述模式解析Android上的任何时区,但不能解析IST。
A quick hack is to modify the timezone part, if there is an IST zone in the string. 如果字符串中有IST区域,则快速修改时区部分。 This works for me also on Android: 这也适用于Android:
String strDate = "Fri Oct 10 23:11:29 IST 2014";
strDate = strDate.replace(" IST ", " GMT+0530 ");
String newPattern = "EEE MMM dd HH:mm:ss Z yyyy";
SimpleDateFormat formatter = new SimpleDateFormat(newPattern, Locale.ENGLISH);
SimpleDateFormat formatter = new SimpleDateFormat(newPattern);
formatter.setLenient(true);
//setting the formatter to be lenient solves the problem //将formatter设置为lenient可以解决问题
Thanks folks for helping.... 谢谢大家的帮助....
use this code it will work.i think there is a problem with IST, it is not recognizing it 使用此代码它将工作。我认为IST存在问题,它无法识别它
public Date parseDate(String strDate) {
strDate = "Fri Oct 10 23:11:29 2014";
String newPattern = "EEE MMM dd HH:mm:ss yyyy";
SimpleDateFormat formatter = new SimpleDateFormat(newPattern);
formatter.setTimeZone(TimeZone.getTimeZone("IST"));
Date date;
try {
date = formatter.parse(strDate.trim());
Log.i("minal", "date:" + date);
return date;
} catch (java.text.ParseException e) {
e.printStackTrace();
Log.d("Populace", "ParseException: " + e.getLocalizedMessage());
}
return null;
}
According to the documentation http://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html , 根据文档http://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html ,
Letter Date or Time Component Presentation Examples
z Time zone General time zone Pacific Standard Time; PST; GMT-08:00
Z Time zone RFC 822 time zone -0800
X Time zone ISO 8601 time zone -08; -0800; -08:00
you are supposed to use small z
instead of capital Z
for the general time zone. 你应该使用小z
代替大写Z
作为一般时区。
However, I tried it (Java SE 1.8.0) and it works both with z
as well as Z
for me. 但是,我尝试了它(Java SE 1.8.0),它既适用于z
,也适用于Z
As Android007 wrote as a comment: "In Java app its working fine but on Android giving exception" - it may be that your library implements the contract more thoroughly than the standard Oracle J2SE :) 正如Android007评论所写的那样:“在Java应用程序中,它的工作正常,但在Android上给予例外” - 可能是您的库比标准的Oracle J2SE更彻底地实现了合同:)
See also here: https://stackoverflow.com/a/2581073/2886891 另见: https : //stackoverflow.com/a/2581073/2886891
Your date pattern contains locale sensitive patterns. 您的日期模式包含区域设置敏感模式。 Since SimpleDateFormat supports the parsing of dates in different locales (ENGLISH, FRENCH, US, JAPANESE... etc.), you'd have to specify which one you are using. 由于SimpleDateFormat支持在不同语言环境(英语,法语,美国,日语......等)中解析日期,因此您必须指定正在使用的日期。
Judging from your "IST", your current default locale on your computer is probably not ENGLISH or US. 从您的“IST”来看,您计算机上当前的默认语言环境可能不是英语或美国。 Since the string "Fri" and "Oct" are in English, the parsing will fail with your default locale. 由于字符串“Fri”和“Oct”是英文的,因此解析将失败并使用您的默认语言环境。
Edit your formatter to the following and it should work: 编辑您的格式化程序,它应该工作:
SimpleDateFormat formatter = new SimpleDateFormat(newPattern, Locale.ENGLISH);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.