【问题标题】:Date format parse exception - "EEE MMM dd HH:mm:ss Z yyyy" [duplicate]日期格式解析异常-“EEE MMM dd HH:mm:ss Z yyyy”[重复]
【发布时间】:2013-11-20 14:46:22
【问题描述】:

我遇到了日期解析示例日期的问题:

SimpleDateFormat parserSDF=new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy", Locale.getDefault());


parserSDF.parse("Wed Oct 16 00:00:00 CEST 2013");

遇到异常

正是我想将此格式日期解析为 yyyy-MM-dd 我试试:

SimpleDateFormat parserSDF = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
Date date = parserSDF.parse("Wed Oct 16 00:00:00 CEST 2013");

采取: java.text.ParseException:无法解析的日期:“Wed Oct 16 00:00:00 CEST 2013”​​


好的,我改用并工作:

SimpleDateFormat parserSDF = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy", Locale.ENGLISH);
Date date = parserSDF.parse("Wed Oct 16 00:00:00 CEST 2013");

【问题讨论】:

标签: java date simpledateformat


【解决方案1】:

我假设你的Locale.getDefault()pl-PL,因为你似乎在波兰。

因此,日期字符串中的英文单词会导致无法解析的日期。

一个合适的波兰日期String 应该类似于

"Wt paź 16 00:00:00 -0500 2013"

否则,请将您的Locale 更改为Locale.ENGLISH,以便SimpleDateFormat 对象可以解析带有英文单词的String 日期。

【讨论】:

    【解决方案2】:

    我认为原始异常是由于您的格式中的Z。 每documentation

    Z   Time zone   RFC 822 time zone   -0800
    

    您很可能打算使用小写 z

    【讨论】:

    • documentation 链接已损坏。
    • @Rao - 修复了链接:)
    • 感谢您的关注和快速修复它。欣赏它。
    【解决方案3】:

    不要使用您和其他人通常不知道哪个默认值的Locale.default,您可以使用locale.ENGLISH 来决定,因为我看到您的字符串日期是英文格式。如果您在其他国家/地区,格式会有所不同。

    这是我的示例代码:

    public static void main(String[] args) {
        try {
            SimpleDateFormat parserSDF = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH);
            Date date = parserSDF.parse("Wed Oct 16 00:00:00 CEST 2013");
            System.out.println("date: " + date.toString());
        } catch (ParseException ex) {
            ex.printStackTrace();
        }
    }
    

    结果将是:date: Wed Oct 16 05:00:00 ICT 2013。或者,您可以通过使用其字段来决定要打印该日期的哪一部分。

    希望有帮助:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-11
      • 1970-01-01
      • 1970-01-01
      • 2021-06-20
      相关资源
      最近更新 更多