应该使用哪种模式来解析java.util.Date的toString返回的日期字符串? [英] What pattern should be used to parse the date string returned by the toString of java.util.Date?

查看:155
本文介绍了应该使用哪种模式来解析java.util.Date的toString返回的日期字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在查看此问题时,我发现OP和接受的答案的代码在运行时会产生 ParseException 。下面是代码:

While looking at this question, I discovered that both the OP's and the accepted answer's code, when run, produce a ParseException. Here is the code:

    String dateString = new java.util.Date().toString();

    System.out.println(dateString);

    SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");

    Date date = format.parse(dateString);

    System.out.println(date.toString());

在仔细检查了打印的日期字符串与提供的格式有何不同之后,我仍然找不到原因他们不匹配。这是打印的日期字符串:

After closely examining how the date string printed differs with the format provided, I still can't find why they don't match. Here is the date string printed:

Sat Aug 19 18:58:41 BST 2017

我的直觉告诉我,这样做不起作用的原因是我的语言环境不同- Locale.getDefualt() 返回 ja_JP

My instincts tell me that the reason why this does not work is that my locale is different - Locale.getDefualt() returns ja_JP.

推荐答案

em> pattern 无关紧要,但是 locale 却没有关系。
Date#toString 使用 Locale.US 和英文名称表示日期,月份和时区,而 SimpleDateFormat(String)使用您的默认语言环境(特别是: Locale.getDefault(Locale.Category.FORMAT))。如果这两个区域设置不匹配,则解析可能会失败,因为不能保证本地名称匹配。

The pattern does not matter, but the locale does. Date#toString uses Locale.US and English names for days, months and time zones, while SimpleDateFormat(String) uses your default locale (specifically: Locale.getDefault(Locale.Category.FORMAT)). If those two locales do not match, parsing may fail as the local names are not guaranteed to match.

所以您应该可以

new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);

JDK 8的摘录:

SimpleDateFormat :

SimpleDateFormat:

public SimpleDateFormat(String pattern)
{
    this(pattern, Locale.getDefault(Locale.Category.FORMAT));
}

日期:

public String toString() {
    // "EEE MMM dd HH:mm:ss zzz yyyy";
    BaseCalendar.Date date = normalize();
    StringBuilder sb = new StringBuilder(28);
    int index = date.getDayOfWeek();
    if (index == BaseCalendar.SUNDAY) {
        index = 8;
    }
    convertToAbbr(sb, wtb[index]).append(' ');                        // EEE
    convertToAbbr(sb, wtb[date.getMonth() - 1 + 2 + 7]).append(' ');  // MMM
    CalendarUtils.sprintf0d(sb, date.getDayOfMonth(), 2).append(' '); // dd

    CalendarUtils.sprintf0d(sb, date.getHours(), 2).append(':');   // HH
    CalendarUtils.sprintf0d(sb, date.getMinutes(), 2).append(':'); // mm
    CalendarUtils.sprintf0d(sb, date.getSeconds(), 2).append(' '); // ss
    TimeZone zi = date.getZone();
    if (zi != null) {
        sb.append(zi.getDisplayName(date.isDaylightTime(), TimeZone.SHORT, Locale.US)); // zzz
    } else {
        sb.append("GMT");
    }
    sb.append(' ').append(date.getYear());  // yyyy
    return sb.toString();
}

[...]

private final static String wtb[] = {
    "am", "pm",
    "monday", "tuesday", "wednesday", "thursday", "friday",
    "saturday", "sunday",
    "january", "february", "march", "april", "may", "june",
    "july", "august", "september", "october", "november", "december",
    "gmt", "ut", "utc", "est", "edt", "cst", "cdt",
    "mst", "mdt", "pst", "pdt"

这篇关于应该使用哪种模式来解析java.util.Date的toString返回的日期字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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