Java 日期格式解析异常 - “EEE MMM dd HH:mm:ss Z yyyy”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19861642/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 20:47:49  来源:igfitidea点击:

Date format parse exception - "EEE MMM dd HH:mm:ss Z yyyy"

javadatesimpledateformat

提问by PDS

I got problem with date parse example date:

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

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


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

got exception

有异常

Exacly I want parse this format date to yyyy-MM-dd I try:

确切地说,我想将此格式日期解析为 yyyy-MM-dd 我尝试:

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

take : java.text.ParseException: Unparseable date: "Wed Oct 16 00:00:00 CEST 2013"

采取:java.text.ParseException:无法解析的日期:“2013 年 10 月 16 日星期三 00:00:00 CEST”



OK I change to and works :

好的,我改为并工作:

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");

采纳答案by Sotirios Delimanolis

I'm going to assume that Locale.getDefault()for you is pl-PLsince you seem to be in Poland.

我将假设Locale.getDefault()对您来说是pl-PL因为您似乎在波兰。

English words in date strings therefore cause an unparseable date.

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

An appropriate Polish date Stringwould be something like

一个合适的波兰日期String是这样的

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

Otherwise, change your Localeto Locale.ENGLISHso that the SimpleDateFormatobject can parse Stringdates with English words.

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

回答by hqt

Instead of using Locale.defaultthat you and others often don't know which default, you can decide by using locale.ENGLISHbecause I see your string date is format in English. If you are at other countries, the format will be different.

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

Here is my example code:

这是我的示例代码:

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();
    }
}

The result will be : date: Wed Oct 16 05:00:00 ICT 2013. Or you can decide which part of this date to be printed, by using its fields.

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

Hope this help :)

希望这有帮助:)

回答by Алексей

I think the original Exception is due to Zin your format. Per documentation:

我认为原始异常是由于Z您的格式。根据文档

Z   Time zone   RFC 822 time zone   -0800

most likely you meant to use lower case z

很可能你打算使用小写 z