奇怪的日期和时间解析结果与SimpleDateFormat [英] Strange date and time parsing result with SimpleDateFormat

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

问题描述

在使用SimpleDateFormat解析ISO8601日期和时间时,我有一个奇怪的问题。相关代码是:

  public class DateHelper 
{
private static SimpleDateFormat iso8601formatter = new SimpleDateFormat( yyyy-MM-dd'T'HH:mm:ssZ);
public static Date parseISO8601(String date)throws ParseException
{
Date result = iso8601formatter.parse(date);
返回结果;
}
}

对于输入,我给它一个字符串



  2010-09-06T15:30:00 + 02:00 

作为一个返回,我得到一个Date对象,日期设置为2010年1月6日,时间为13:30,时区GMT + 00:00。



编辑:我也尝试使用2010-09-06T15:30:00 + 0200,结果相同。



事情是,设置的日期是部分正确的,只是该月份被错误地设置。



该问题显示在Android 1.6和Android 2.2上。



如何解决?

解决方案

如果您使用 mm 为月,而不是 MM



所以我怀疑导致问题的原因是,您没有运行您认为正在运行的代码版本。按照您的问题重新编译并重新执行代码。这是正确的。


I have a strange problem when parsing a ISO8601 date and time with SimpleDateFormat. The relevant code is:

public class DateHelper
{
    private static SimpleDateFormat iso8601formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
    public static Date parseISO8601(String date) throws ParseException
    {
        Date result = iso8601formatter.parse(date);
        return result;
    }
}

For input I'm giving it a string

2010-09-06T15:30:00+02:00

And as a return I get a Date object with date set to 6th of January 2010 with time of 13:30 and timezone of GMT+00:00.

EDIT: I also tried using "2010-09-06T15:30:00+0200" with same results.

Confusing thing is, that the date set is partially correct, just the month is set wrongly.

The issue is shown on Android 1.6 and Android 2.2.

How can I fix it?

解决方案

Your problem is reproduceable if you use mm for month instead of MM.

So I suspect that the cause of the problem is there and that you're not running the version of the code you think you're running. Recompile and reexecute the code as you've in your question. It's correct.

这篇关于奇怪的日期和时间解析结果与SimpleDateFormat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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