我正在尝试解析日期,但出现时区异常

I am trying to parse date but I got timezone exception

这是我的日期字符串 "Thu Feb 25 12:58:28 MST 2016",我使用这个格式 "E MMM dd HH:mm:ss Z yyyy" 进行解析。

但是我收到日期解析错误。

我该怎么办?

我正在使用以下函数解析日期字符串

public static String getDate(String targrtDate) {
    String dateStr = targrtDate;
    DateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
    Date date = null;
    try {
        date = (Date) formatter.parse(dateStr);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    System.out.println(date);

    if (date == null) {
        return "";
    }

    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    String formatedDate = cal.get(Calendar.DATE) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.YEAR);
    String formatedTime = cal.get(Calendar.HOUR) + "/" + cal.get(Calendar.MINUTE);

    Log.i("formatedDate", "" + formatedDate + " " + formatedTime);

    return formatedDate;
}

我已经解决了我的日期解析问题。

如果有人使用 MST 时区并且出现解析错误,请将 SimpleDateFormat() 的第二个参数添加为

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

所以我的最终代码将是

public static String getDate(String targrtDate) {
    String dateStr = targrtDate;
    SimpleDateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy", Locale.US);
    Date date = null;
    try {
        date = (Date) formatter.parse(dateStr);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    System.out.println(date);

    if (date == null) {
        return "";
    }

    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    String formatedDate = cal.get(Calendar.DATE) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.YEAR);
    String formatedTime = cal.get(Calendar.HOUR) + ":" + cal.get(Calendar.MINUTE);

    Log.i("formatedDate", "" + formatedDate + " " + formatedTime);

    return formatedDate;
}

特别感谢@MenoHochschild

tl;博士

ZonedDateTime.parse( 
    "Thu Feb 25 12:58:28 MST 2016" , 
    DateTimeFormatter.ofPattern( "E MMM dd HH:mm:ss z uuuu" , Locale.US ) 
)

2016-02-25T12:58:28-07:00[America/Denver]

java.time

现代方法使用 java.time 类。对于 Android,请参阅下面的最后一个项目符号。

String input =  "Thu Feb 25 12:58:28 MST 2016" ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "E MMM dd HH:mm:ss z uuuu" , Locale.US ) ;
ZonedDateTime zdt = ZonedDateTime.parse( input , f ) ;

在 IdeOne.com 查看此代码 运行。 (但请注意,该站点上的 JVM 已硬连线到 Locale.US,任何其他的都将被忽略。)

zdt.toString(): 2016-02-25T12:58:28-07:00[America/Denver]

您输入的字符串格式不正确。如果可能,更改为使用标准 ISO 8601 格式。标准格式实用,易于机器解析,易于跨文化的人类阅读。方便的是,这些格式在 java.time 类 when parsing/generating 字符串中默认使用。

指定 proper time zone name in the format of continent/region, such as America/Montreal, Africa/CasablancaPacific/Auckland。切勿使用 MSTESTIST 等 3-4 个字母的缩写,因为它们 不是 真实时区,未标准化,也不是甚至是独一无二的(!)。

ZoneId zEdmonton = ZoneId.of( "America/Edmonton" ) ;
ZoneId zDenver = ZoneId.of( "America/Denver" ) ;
ZoneId zMazatlan = ZoneId.of( "America/Mazatlan" ) ;

关于java.time

java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

Joda-Time project, now in maintenance mode, advises migration to the java.time 类.

要了解更多信息,请参阅 Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310

在哪里获取java.time类?