android / java se SimpleDateFormat TimeZone 问题

发布于 2024-11-10 13:11:14 字数 2103 浏览 4 评论 0原文

我在 android 2.1 上运行的程序中遇到了一个奇怪的问题,对我来说这似乎是 android 中的一个错误。请告诉我这里发生了什么,因为我迷路了。 :)

以下程序:

import java.util.*;
import java.text.*;

class TestMe {
    public static void main(String[] args) {
            String time = "2010-08-01T18:00:00+0000";

            TimeZone tg = TimeZone.getDefault();
            System.out.println(tg.getID());

            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");

            try {
                Date indate = formatter.parse(time);

                String outdate = formatter.format(indate);
                System.out.println(outdate);

                formatter.setTimeZone( TimeZone.getTimeZone("UTC"));

                String ooutdate = formatter.format(indate);
                    System.out.println(ooutdate);

                formatter.setTimeZone( TimeZone.getDefault() );
                Date ioutdate = formatter.parse(ooutdate);
                System.out.println(formatter.format(ioutdate));

                } catch( Exception e ) {
                    e.printStackTrace();
            }
        }
        }

在 mac 上使用最新的 java se 时返回此内容:

(~) % javac -g TestMe.java && java TestMe                                                                                                                                                                                                              
Europe/Berlin
2010-08-01T20:00:00+0200
2010-08-01T18:00:00+0000
2010-08-01T20:00:00+0200

但是当我在 2.1 上的 android 应用程序中的方法内运行它时返回以下内容:

I/System.out( 5379): ------------------------------------------------------------------
I/System.out( 5379): Europe/Berlin
I/System.out( 5379): 2010-08-01T20:00:00+0200
I/System.out( 5379): 2010-08-01T18:00:00+0100
I/System.out( 5379): 2010-08-01T18:00:00+0100
I/System.out( 5379): ------------------------------------------------------------------

有人知道这个特定的 android api 修订版与 TimeZone 和日期解析?这对我来说是一个非常奇怪的问题,因为 android 版本应该返回与 java se 版本完全相同的结果,但事实并非如此。

感谢您的任何提示,

卡斯滕

I'm encountering a strange issue in a program running on android 2.1 which seems like a bug in android to me. Please enlighten me as to what is going here, as I'm lost. :)

The following program:

import java.util.*;
import java.text.*;

class TestMe {
    public static void main(String[] args) {
            String time = "2010-08-01T18:00:00+0000";

            TimeZone tg = TimeZone.getDefault();
            System.out.println(tg.getID());

            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");

            try {
                Date indate = formatter.parse(time);

                String outdate = formatter.format(indate);
                System.out.println(outdate);

                formatter.setTimeZone( TimeZone.getTimeZone("UTC"));

                String ooutdate = formatter.format(indate);
                    System.out.println(ooutdate);

                formatter.setTimeZone( TimeZone.getDefault() );
                Date ioutdate = formatter.parse(ooutdate);
                System.out.println(formatter.format(ioutdate));

                } catch( Exception e ) {
                    e.printStackTrace();
            }
        }
        }

returns this, when using latest java se on the mac:

(~) % javac -g TestMe.java && java TestMe                                                                                                                                                                                                              
Europe/Berlin
2010-08-01T20:00:00+0200
2010-08-01T18:00:00+0000
2010-08-01T20:00:00+0200

but returns the following when I run it inside a method in my android app on 2.1 :

I/System.out( 5379): ------------------------------------------------------------------
I/System.out( 5379): Europe/Berlin
I/System.out( 5379): 2010-08-01T20:00:00+0200
I/System.out( 5379): 2010-08-01T18:00:00+0100
I/System.out( 5379): 2010-08-01T18:00:00+0100
I/System.out( 5379): ------------------------------------------------------------------

Is anybody aware of problems in this specific android api revision with TimeZone and Date parsing? This is a very strange issue to me, as it the android version should return exactly the same as the java se version but doesn't.

Thanks for any hints,

Karsten

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

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

发布评论

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

评论(5

烙印 2024-11-17 13:11:14

尝试将 setTimeZone("UTC") 替换为 setTimeZone("GMT") 它可能是不同的实现,因为现在 BST(英国夏令时)tz 中的 0 子午线是格林威治标准时间+1。

Try to replace setTimeZone("UTC") with setTimeZone("GMT") it might be different implementations as now 0-meridian in BST(British summer time) tz which is GMT+1.

手心的海 2024-11-17 13:11:14

是的,我尝试了一下,得到了和你一样的结果。我想那是一个错误。

http://code.google.com/p/android/issues/detail ?id=8258 可能会帮助你

Yes, I tried it and got the same result as you did. I guess its a bug then.

http://code.google.com/p/android/issues/detail?id=8258 might help you out

心房敞 2024-11-17 13:11:14

我已经在windows java和android2.3中进行了测试。我在这两种情况下都得到相同的结果。我认为这个bug在android 2.3中就不再存在了。反正
请检查 android 中的导入语句与 java 中的

导入语句 android

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import statements in java

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

谢谢
迪帕克

I have tested in windows java and android2.3. I am getting the same result in both of the case. I think the bug is no more in android 2.3. Anyway
please check the import statements for android versus that of java

import statements in android

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import statements in java

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

Thanks
Deepak

江城子 2024-11-17 13:11:14

我设法使用 Calendar 对象完成预期的日期转换:

        try{ 
        String time = "2010-08-01T18:00:00+0000";

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
        Date indate = formatter.parse(time);

        Calendar cal = Calendar.getInstance();
        System.out.println("time right now: " + cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-" + cal.get(Calendar.DAY_OF_MONTH) + "T" + cal.get(Calendar.HOUR_OF_DAY) + ":" + cal.get(Calendar.MINUTE) + ":" + cal.get(Calendar.SECOND) + "Z");

        cal.setTime( indate );

        System.out.println("time from date string: " + cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-" + cal.get(Calendar.DAY_OF_MONTH) + "T" + cal.get(Calendar.HOUR_OF_DAY) + ":" + cal.get(Calendar.MINUTE) + ":" + cal.get(Calendar.SECOND) + "Z");

        Calendar utcCal = new GregorianCalendar( TimeZone.getTimeZone("UTC"));
        utcCal.setTimeInMillis( cal.getTimeInMillis() );

        System.out.println("time from date string in utc: " + utcCal.get(Calendar.YEAR) + "-" + (utcCal.get(Calendar.MONTH) + 1) + "-" + utcCal.get(Calendar.DAY_OF_MONTH) + "T" + utcCal.get(Calendar.HOUR_OF_DAY) + ":" + utcCal.get(Calendar.MINUTE) + ":" + utcCal.get(Calendar.SECOND) + "Z");

    } catch( Exception e) {

    }

I managed to get the intended date conversion done using the Calendar object:

        try{ 
        String time = "2010-08-01T18:00:00+0000";

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
        Date indate = formatter.parse(time);

        Calendar cal = Calendar.getInstance();
        System.out.println("time right now: " + cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-" + cal.get(Calendar.DAY_OF_MONTH) + "T" + cal.get(Calendar.HOUR_OF_DAY) + ":" + cal.get(Calendar.MINUTE) + ":" + cal.get(Calendar.SECOND) + "Z");

        cal.setTime( indate );

        System.out.println("time from date string: " + cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-" + cal.get(Calendar.DAY_OF_MONTH) + "T" + cal.get(Calendar.HOUR_OF_DAY) + ":" + cal.get(Calendar.MINUTE) + ":" + cal.get(Calendar.SECOND) + "Z");

        Calendar utcCal = new GregorianCalendar( TimeZone.getTimeZone("UTC"));
        utcCal.setTimeInMillis( cal.getTimeInMillis() );

        System.out.println("time from date string in utc: " + utcCal.get(Calendar.YEAR) + "-" + (utcCal.get(Calendar.MONTH) + 1) + "-" + utcCal.get(Calendar.DAY_OF_MONTH) + "T" + utcCal.get(Calendar.HOUR_OF_DAY) + ":" + utcCal.get(Calendar.MINUTE) + ":" + utcCal.get(Calendar.SECOND) + "Z");

    } catch( Exception e) {

    }
川水往事 2024-11-17 13:11:14

尝试在每次 setTimeZone() / format() 调用之前重新创建 SimpleDateFormat 对象。

这解决了我在使用通用 SimpleDateFormat 对象格式化一组跨越 DST 更改的时间时遇到的问题。如果集合中的第一个时间在 DST 阈值之后,则集合中的所有时间的格式均正确。如果集合中的第一次是在 DST 阈值之前,那么 DST 阈值之后的所有时间都会被错误地格式化(即超出一小时) - 即使我尝试在每次 format() 调用之前调用 setTimeZone() ,这种情况也会继续。

我从中推断出,SimpleDateFormat 对象使用的时区与第一个 format() 调用相关,并且对 setTimeZone() 的后续调用实际上被忽略或覆盖(在我看来,这听起来像是一个 Java 问题,而不是一个安卓问题)。

Try recreating your SimpleDateFormat object before each setTimeZone() / format() call.

This fixed an issue I was having when using a common SimpleDateFormat object to format a set of times that spanned a DST change. If the first time in the set was after the DST threshold, all times in the set were formatted correctly. If the first time in the set was before the DST threshold, then all times after the DST threshold were incorrectly formatted (i.e. were one hour out) - and this continued even when I tried calling setTimeZone() before every format() call.

What I inferred from this is that the time zone used by the SimpleDateFormat object is tied to the first format() call, and subsequent calls to setTimeZone() are effectively ignored or overwritten (which sounds to me like a Java issue, rather than an Android issue).

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