本文介绍了Java - 不可稀疏的日期,需要格式匹配“GMT-0400”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下Java:

DateFormat formatter = new SimpleDateFormat(
    "EEE MMM dd yyyy HH:mm:ss zZ (zzzz)", Locale.ENGLISH);
Calendar cal = Calendar.getInstance();
cal.set(2011, Calendar.APRIL, 1);
out.println(formatter.format(cal.getTime()));
out.println();

Date date;
try {
    date = formatter
        .parse("Fri Apr 01 2011 00:00:00 GMT-0400 (Eastern Daylight Time)");
} catch (ParseException e) {
    out.println("Failed to parse date: " + e.getMessage());
    e.printStackTrace(out);
}

这是一个servlet,而 / code> -constructed date显示为:

This is in a servlet, and the Calendar-constructed date comes out as:

这个外观与日期字符串的格式一样,除了 EDT-0400 与所需的 GMT-0400 。当尝试解析日期字符串时,代码将失败:

This looks like the same format as the date string I'm trying to parse, except for EDT-0400 versus the desired GMT-0400. The code fails when trying to parse the date string:

如何解析这样一个字符串?这是来自Sencha Touch 1.1.1模型中的JavaScript日期,存储在WebSQL本地存储中。

How can I parse such a string? This is coming from a JavaScript date in a Sencha Touch 1.1.1 model, stored in WebSQL local storage.

推荐答案

由于某些原因GMT-0400不工作,UTC-0400正在工作。您可以用UTC替换GMT。

For some reason GMT-0400 isnt' working, and UTC-0400 is working. You can replace GMT with UTC.

请注意,这部分将被完全忽略 - 时区将从括号中找到(至少在我的机器上,JDK 6)

Note that this part will be completely ignored - the timezone will be resolved from what's found in the brackets (at least on my machine, JDK 6)

这篇关于Java - 不可稀疏的日期,需要格式匹配“GMT-0400”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 00:54