使用SimpleDateFormat解析时间

时间:2012-09-28 07:15:03

标签: java date time simpledateformat

我正在尝试解析这样的日期:2012年8月28日星期二21:16:23 +0000使用此代码:

SimpleDateFormat format = new SimpleDateFormat("E M dd HH:mm:ssZ yyyy", Locale.ENGLISH);
String d = object.getString("created_at"); // d = Tue Aug 28 21:16:23 +0000 2012;
date = format.parse(d);

但有例外:

09-28 11:10:24.471: W/System.err(10388): java.text.ParseException: Unparseable date: "Fri Sep 28 07:09:09 +0000 2012" (at offset 4)

我犯了哪个错误?

2 个答案:

答案 0 :(得分:2)

试试这个

SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH);

MMM用于表示短月。

答案 1 :(得分:2)

你需要MMM作为8月份的月份代表。

SimpleDateFormat format = new SimpleDateFormat("E MMM dd HH:mm:ssZ yyyy", Locale.ENGLISH);
        String d = "Tue Aug 28 21:16:23 +0000 2012"; // d =;
        Date date = format.parse(d);
                System.out.println(date);

输出:2012年8月28日星期二22:16:23

查看SimpleDateFormat's javadoc可能有所帮助,模式字符串有一些有用的示例。