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
Date format parse exception - "EEE MMM dd HH:mm:ss Z yyyy"
提问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-PL
since you seem to be in Poland.
我将假设Locale.getDefault()
对您来说是pl-PL
因为您似乎在波兰。
English words in date strings therefore cause an unparseable date.
因此,日期字符串中的英语单词会导致无法解析的日期。
An appropriate Polish date String
would be something like
一个合适的波兰日期String
是这样的
"Wt pa? 16 00:00:00 -0500 2013"
Otherwise, change your Locale
to Locale.ENGLISH
so that the SimpleDateFormat
object can parse String
dates with English words.
否则,将您更改Locale
为,Locale.ENGLISH
以便SimpleDateFormat
对象可以String
使用英文单词解析日期。
回答by hqt
Instead of using Locale.default
that you and others often don't know which default, you can decide by using locale.ENGLISH
because 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 Z
in your format.
Per documentation:
我认为原始异常是由于Z
您的格式。根据文档:
Z Time zone RFC 822 time zone -0800
most likely you meant to use lower case z
很可能你打算使用小写 z