简体   繁体   English

在 android 中解析日期

[英]Parse date in android

I have the following code to pase a date我有以下代码来粘贴日期

DateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy kk:mm:ss", Locale.getDefault());
Date _pubDate =  df.parse(_pubDateE.getFirstChild().getNodeValue());

But I get this error:但我得到这个错误:

java.text.ParseException: Unparseable date: "Fri, 12 Aug 2011 15:34:47 CEST" java.text.ParseException:无法解析的日期:“2011 年 8 月 12 日星期五 15:34:47 CEST”

What is wrong?怎么了?

You're missing the timezone in the date format at the end, in your exception message, the "CEST" part.您在异常消息“CEST”部分最后缺少日期格式的时区。 Your code你的代码

DateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy kk:mm:ss", Locale.getDefault());

should be应该

DateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy kk:mm:ss z", Locale.getDefault());

You might want to read SimpleDateFormat您可能想阅读SimpleDateFormat

Edit At the bottom of this page, the timezone format is more cleary explained Clearer Timezone format编辑在此页面的底部,时区格式解释更清楚更 清晰的时区格式

I think you need to add zzz in the end (for timezone):我认为您需要在最后添加zzz (对于时区):

"EEE, dd MMM yyyy kk:mm:ss zzz"

The date string is not in the format you specified.日期字符串不是您指定的格式。 Notice the time zone at the end?注意到最后的时区了吗?

You probably want: new SimpleDateFormat("EEE, dd MMM yyyy kk:mm:ss zzz", Locale.getDefault());您可能想要: new SimpleDateFormat("EEE, dd MMM yyyy kk:mm:ss zzz", Locale.getDefault());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM