使用 SimpleDateFormat 解析奇怪的日期和时间结果

发布于 2024-10-01 07:18:21 字数 667 浏览 10 评论 0原文

使用 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);
        return result;
    }
}

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

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 上。

我该如何修复它?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

太傻旳人生 2024-10-08 07:18:21

如果您使用 mm 表示月份而不是 MM,您的问题是可以重现的。

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

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.

甜柠檬 2024-10-08 07:18:21

可能不是你的问题,但因为 SimpleDateFormat 不是线程安全的 您需要将代码更改为:

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

Probably not your problem but since SimpleDateFormat is not thread safe you need to change your code to:

public class DateHelper
{
    public static Date parseISO8601(String date) throws ParseException
    {
        return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").parse(date);
    }
}
時窥 2024-10-08 07:18:21

您正在处理 ParseException 吗?代码应该为该输入字符串抛出 ParseException。
不确定 Android 的情况,但对于 Java 6,输入字符串 2010-09-06T15:30:00+02:00 无效。

+02:00 不是有效的时区。
尝试不使用冒号:

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

技巧(测试)是使用 DateFormat 格式化日期并检查创建的字符串:

    System.out.println(iso8601formatter.format(new Date()));  

Are you handling the ParseException? The code should be throwing a ParseException for that input string.
Not sure about Android, but with Java 6 the input string 2010-09-06T15:30:00+02:00 is not valid.

+02:00 is not a valid timezone.
Try it without the colon:

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

A trick (test) is to use the DateFormat to format a Date and check the created String:

    System.out.println(iso8601formatter.format(new Date()));  
愿得七秒忆 2024-10-08 07:18:21

我创建了一个 1.6 项目,代码对我有用...

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;
}

public static String makeISO8601String(Date date) {
    return iso8601formatter.format(date);
}

}

<代码>

public class StackOverflowTest extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    TextView textView = (TextView)this.findViewById(R.id.textView);

    StringBuilder builder = new StringBuilder();
    try {
        String badDateString = "2010-09-06T15:30:00+0200";
        Date parsedDate = DateHelper.parseISO8601(badDateString);
        builder.append(badDateString).append("\n");
        builder.append(parsedDate.toString()).append("\n");
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    String dateString = DateHelper.makeISO8601String(new Date());
    builder.append("now="+dateString).append("\n");

    textView.setText(builder.toString());
}

}

对我来说,它打印出:

2010-09-06T15:30:00+0200
Mon Sep 06 06:30:00 PDT 2010
now=2010-11-06T14:41:55-077

这对我来说都是正确的......

I created a 1.6 project and the code worked for me...

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;
}

public static String makeISO8601String(Date date) {
    return iso8601formatter.format(date);
}

}

public class StackOverflowTest extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    TextView textView = (TextView)this.findViewById(R.id.textView);

    StringBuilder builder = new StringBuilder();
    try {
        String badDateString = "2010-09-06T15:30:00+0200";
        Date parsedDate = DateHelper.parseISO8601(badDateString);
        builder.append(badDateString).append("\n");
        builder.append(parsedDate.toString()).append("\n");
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    String dateString = DateHelper.makeISO8601String(new Date());
    builder.append("now="+dateString).append("\n");

    textView.setText(builder.toString());
}

}

For me it prints out:

2010-09-06T15:30:00+0200
Mon Sep 06 06:30:00 PDT 2010
now=2010-11-06T14:41:55-077

Which is all correct for me...

ι不睡觉的鱼゛ 2024-10-08 07:18:21

为了解析您在解析时区(如 +02:00)时应该使用的第一个字符串,

public static Date parseDateFromString(String aDateString){
    SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz");
    Date date=null;
    try {
        date= inputFormat.parse(aDateString);
    } catch (ParseException e) {
        return null;
    }
    return date;

它使用 z 而不是 Z。希望它有帮助。我认为给你 +00:00 的问题与你使用的区域设置有关。你应该尝试用不同的语言环境来测试它。

for parsing the first string u should use

public static Date parseDateFromString(String aDateString){
    SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz");
    Date date=null;
    try {
        date= inputFormat.parse(aDateString);
    } catch (ParseException e) {
        return null;
    }
    return date;

when parsing the Timezone like +02:00 it is with z instead of Z. hope it helps. and the issue with giving you +00:00 i think is connected with the locale you use. u should try to test it with a different locale.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文