简体   繁体   English

无法解释的日期:“2014年10月10日星期二23:11:07”(抵消20日)

[英]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.

相关问题 java.text.ParseException:无法解析的日期“ IST 2014年9月12日23:22:46 - java.text.ParseException: Unparseable date “Fri Sep 12 23:22:46 IST 2014” 从另一个java.util.Date对象获取java.util.Date对象为(10/7/14)(IST 2014年10月7日11:21:00) - Getting java.util.Date Object As (10/7/14 ) From Another java.util.Date Object (Tue Oct 07 11:21:00 IST 2014) 无法解析的日期:“2013-07-11T13:41:22.000Z”(偏移量 23) - Unparseable date: "2013-07-11T13:41:22.000Z" (at offset 23) 无法解析的日期:1975年7月23日 - Unparseable date: 07/23/1975 java.text.ParseException:无法解析的日期:“2014 年 5 月 13 日星期二 23:58:30 IST” - java.text.ParseException: Unparseable date: "Tue May 13 23:58:30 IST 2014" java.text.ParseException:无法解析的日期:“ 2018年10月7日,星期日,11:00 PM” - java.text.ParseException: Unparseable date: “11:00 PM, Sun 07 Oct 2018” Android - 如何解析此日期[2014-04-23T14:20:00.000-07:00]? - Android - How to parse this date [ 2014-04-23T14:20:00.000-07:00 ]? java.text.ParseException:不可解析的日期:“ 20年1月20日,上午10:28”(偏移量为0) - java.text.ParseException: Unparseable date: “20, 1 20, 2016 10:28 am” (at offset 0) 无法解析的日期异常,无法找到2014-11-28T13:46:23-08:00的模式 - Unparseable date exception, can't find pattern for 2014-11-28T13:46:23-08:00 java.text.ParseException:无法解析的日期:“ 11/11/2014” - java.text.ParseException: Unparseable date: “11/11/2014”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM