解析SimpleDateFormat [英] Parsing SimpleDateFormat

查看:84
本文介绍了解析SimpleDateFormat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个日期,我似乎无法正确解析。

I have this date that I seem to be unable to parse correctly.

字符串文本Wed May 21 05:44:09 -0700 2014;

String text "Wed May 21 05:44:09 -0700 2014";

这是我的日期格式

public static final String DATE_FORMAT_PATTERN = "EEE MMM dd HH:mm:ss Z yyyy";

我正在尝试使用SimpleDateFormat来解析它。

I am trying to use a SimpleDateFormat to parse it.

错误字符串是不可解析的。

Error string is unparsable.

我在这里做错了什么。

What am I doing wrong here.

请注意,这不是重复。具有强制语言环境的解决方案在另一个问题中没有描述。

Note that this is not a duplicate. The solution with forcing locale is not described in the other question.

推荐答案

要解析您的日期,您可以使用

To parse your date you can use

SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_PATTERN);
Date parsedDate = sdf.parse("Wed May 21 05:44:09 -0700 2014");

但如果失败并且你看到了

But if that fails and you are seeing

java.text.ParseException:Unparseable date:Wed May 21 05:44:09 -0700 2014

java.text.ParseException: Unparseable date: "Wed May 21 05:44:09 -0700 2014"

然后很可能 Wed 被默认语言环境识别为正确的日期。在这种情况下,您必须将区域设置设置为识别此单词的位置,例如

then most probably Wed is not recognised by your default locale as correct day. In that case you will have to set locale to place where this word is recognized, like

SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_PATTERN, Locale.US);
//                                                               ^^^^^^^^^

这篇关于解析SimpleDateFormat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆