使用SimpleDateFormatter的 "不可解析的日期 "与API代码示例[英] "Unparseable date" using SimpleDateFormatter with API code example

本文是小编为大家收集整理的关于使用SimpleDateFormatter的 "不可解析的日期 "与API代码示例的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在尝试解析日期,例如"Sat, 29 Dec 2012 04:07:09 +0100" 我正在使用SimpleDateFormatter和格式设置为

new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.getDefault());

不幸的是我得到

12-29 04:44:29.890: E/MainActivity(3995): Unparseable date: "Sat, 29 Dec 2012 04:07:09 +0100" (at offset 0)
12-29 04:44:29.890: E/MainActivity(3995): java.text.ParseException: Unparseable date: "Sat, 29 Dec 2012 04:07:09 +0100" (at offset 0)

我绝对不会得到,因为该参数正则表达式是复制并直接从文档中粘贴的 http://docs.oracle.com/javase/6/docs/api/java/java/text/simpledateformat.html 在示例下

谢谢!

推荐答案

无论Locale.getDefault()返回的语言环境都不支持以这种方式格式化日期.

例如,将其更改为Locale.US将起作用.

本文地址:https://www.itbaoku.cn/post/102607.html

问题描述

I am trying to parse dates, for example "Sat, 29 Dec 2012 04:07:09 +0100" I am using SimpleDateFormatter with format set as

new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.getDefault());

Unfortunately I am getting

12-29 04:44:29.890: E/MainActivity(3995): Unparseable date: "Sat, 29 Dec 2012 04:07:09 +0100" (at offset 0)
12-29 04:44:29.890: E/MainActivity(3995): java.text.ParseException: Unparseable date: "Sat, 29 Dec 2012 04:07:09 +0100" (at offset 0)

Which I absolutely dont get as that parameter regular expression is copy and pasted directly from documentation http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html under Examples

Thanks!

推荐答案

Whatever the locale returned by Locale.getDefault() is doesn't support dates formatted that way.

Changing it to Locale.US for example, will work.