Android 中的时区问题

发布于 2024-12-08 18:08:27 字数 3388 浏览 0 评论 0原文

我的时间以毫秒为单位,我在应用程序中以 "MM/dd/yyyy HH:mm:ss" 格式显示该时间,它工作正常。

问题:如果我更改 Android 设备中的时区,它在我的应用程序中显示不同的时间可能是因为某些时区计算。我想显示数据库中与任何时区相同的时间。

我正在使用下面的代码来测试时区问题

Locale locale = new Locale("en", "IN");

TimeZone tz = TimeZone.getDefault();
System.out.println("Default timezon id :: " + tz.getID());
tz.setID("Asia/Calcutta");
String timezon = tz.getDisplayName(false, TimeZone.SHORT, locale);
System.out.println("Timezon id :: " + tz.getID() + " Timezon name :: " + timezon);
Calendar cal = Calendar.getInstance(tz, locale);

System.out.println("TImezon :: " + tz);
// "1319084400775" is the milliseconds of 10/20/2011 05:20:00 in MM/dd/yyyy HH:mm:ss format 
Date date = new Date(Long.parseLong("1319084400775"));
System.out.println("Date :: " + date.toGMTString());
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", locale);

System.out.println("Start date :: " + format.format(date));
// "1319084400775" is the milliseconds of 10/20/2011 05:20:00 in MM/dd/yyyy HH:mm:ss format 
cal.setTimeInMillis(Long.parseLong("1319084400775"));

timezon = cal.getTimeZone().getDisplayName(false, TimeZone.SHORT, locale);
System.out.println("Calender's timezon :: " + timezon);
System.out.println("Start date :: yyyy-mm-dd hh:mm:ss " + cal.get(Calendar.YEAR) + "-" + cal.get(Calendar.MONTH) + "-" + cal.get(Calendar.DAY_OF_MONTH) + " " + cal.get(Calendar.HOUR_OF_DAY) + ":" + cal.get(Calendar.MINUTE));
// "1319084700776" is the milliseconds of 10/20/2011 05:25:00 in MM/dd/yyyy HH:mm:ss format 
cal.setTimeInMillis(Long.parseLong("1319084700776"));
System.out.println("End date :: " + cal.getTime());

输出 ::

如果我为“太平洋/斐济”设置时区;

10-07 17:03:40.392: INFO/System.out(1193): Default timezon id :: Pacific/Fiji
10-07 17:03:40.392: INFO/System.out(1193): Timezon id :: Asia/Calcutta Timezon name :: GMT+05:30
10-07 17:03:40.406: INFO/System.out(1193): TImezon :: org.apache.harmony.luni.internal.util.ZoneInfo@7b42d3a7
10-07 17:03:40.422: INFO/System.out(1193): Date :: 20 Oct 2011 04:20:00 GMT
10-07 17:03:40.442: INFO/System.out(1193): Start date :: 2011-10-20 16:20:00
10-07 17:03:40.442: INFO/System.out(1193): Calender's timezon :: GMT+05:30
10-07 17:03:40.442: INFO/System.out(1193): Start date :: yyyy-mm-dd hh:mm:ss 2011-9-20 16:20
10-07 17:03:40.452: INFO/System.out(1193): End date :: Thu Oct 20 16:25:00 Pacific/Fiji 2011

如果我设置“America/Tijuana”的时区;

10-06 22:05:20.702: INFO/System.out(1193): Default timezon id :: America/Tijuana
10-06 22:05:20.712: INFO/System.out(1193): Timezon id :: Asia/Calcutta Timezon name :: GMT+05:30
10-06 22:05:20.712: INFO/System.out(1193): TImezon :: org.apache.harmony.luni.internal.util.ZoneInfo@1294e658
10-06 22:05:20.733: INFO/System.out(1193): Date :: 20 Oct 2011 04:20:00 GMT
10-06 22:05:20.742: INFO/System.out(1193): Start date :: 2011-10-19 21:20:00
10-06 22:05:20.742: INFO/System.out(1193): Calender's timezon :: GMT+05:30
10-06 22:05:20.752: INFO/System.out(1193): Start date :: yyyy-mm-dd hh:mm:ss 2011-9-19 21:20
10-06 22:05:20.752: INFO/System.out(1193): End date :: Wed Oct 19 21:25:00 America/Tijuana 2011

编辑

我现在的时间以毫秒为单位,我在应用程序中以日期格式显示该毫秒,但问题是当我更改设备中的时区时据此获取日期时区。

I have time in milliseconds and I am displaying that time in my application in "MM/dd/yyyy HH:mm:ss" format, it is working fine.

Problem: if I change the timezone in android device it display different time in my application may be because of some timezone calculation. I want to display same time that is in my database as it is in any timezone.

I am using below code to test timezone issue

Locale locale = new Locale("en", "IN");

TimeZone tz = TimeZone.getDefault();
System.out.println("Default timezon id :: " + tz.getID());
tz.setID("Asia/Calcutta");
String timezon = tz.getDisplayName(false, TimeZone.SHORT, locale);
System.out.println("Timezon id :: " + tz.getID() + " Timezon name :: " + timezon);
Calendar cal = Calendar.getInstance(tz, locale);

System.out.println("TImezon :: " + tz);
// "1319084400775" is the milliseconds of 10/20/2011 05:20:00 in MM/dd/yyyy HH:mm:ss format 
Date date = new Date(Long.parseLong("1319084400775"));
System.out.println("Date :: " + date.toGMTString());
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", locale);

System.out.println("Start date :: " + format.format(date));
// "1319084400775" is the milliseconds of 10/20/2011 05:20:00 in MM/dd/yyyy HH:mm:ss format 
cal.setTimeInMillis(Long.parseLong("1319084400775"));

timezon = cal.getTimeZone().getDisplayName(false, TimeZone.SHORT, locale);
System.out.println("Calender's timezon :: " + timezon);
System.out.println("Start date :: yyyy-mm-dd hh:mm:ss " + cal.get(Calendar.YEAR) + "-" + cal.get(Calendar.MONTH) + "-" + cal.get(Calendar.DAY_OF_MONTH) + " " + cal.get(Calendar.HOUR_OF_DAY) + ":" + cal.get(Calendar.MINUTE));
// "1319084700776" is the milliseconds of 10/20/2011 05:25:00 in MM/dd/yyyy HH:mm:ss format 
cal.setTimeInMillis(Long.parseLong("1319084700776"));
System.out.println("End date :: " + cal.getTime());

Output ::

If I set timezone for "Pacific/Fiji";

10-07 17:03:40.392: INFO/System.out(1193): Default timezon id :: Pacific/Fiji
10-07 17:03:40.392: INFO/System.out(1193): Timezon id :: Asia/Calcutta Timezon name :: GMT+05:30
10-07 17:03:40.406: INFO/System.out(1193): TImezon :: org.apache.harmony.luni.internal.util.ZoneInfo@7b42d3a7
10-07 17:03:40.422: INFO/System.out(1193): Date :: 20 Oct 2011 04:20:00 GMT
10-07 17:03:40.442: INFO/System.out(1193): Start date :: 2011-10-20 16:20:00
10-07 17:03:40.442: INFO/System.out(1193): Calender's timezon :: GMT+05:30
10-07 17:03:40.442: INFO/System.out(1193): Start date :: yyyy-mm-dd hh:mm:ss 2011-9-20 16:20
10-07 17:03:40.452: INFO/System.out(1193): End date :: Thu Oct 20 16:25:00 Pacific/Fiji 2011

If I set timezone for "America/Tijuana";

10-06 22:05:20.702: INFO/System.out(1193): Default timezon id :: America/Tijuana
10-06 22:05:20.712: INFO/System.out(1193): Timezon id :: Asia/Calcutta Timezon name :: GMT+05:30
10-06 22:05:20.712: INFO/System.out(1193): TImezon :: org.apache.harmony.luni.internal.util.ZoneInfo@1294e658
10-06 22:05:20.733: INFO/System.out(1193): Date :: 20 Oct 2011 04:20:00 GMT
10-06 22:05:20.742: INFO/System.out(1193): Start date :: 2011-10-19 21:20:00
10-06 22:05:20.742: INFO/System.out(1193): Calender's timezon :: GMT+05:30
10-06 22:05:20.752: INFO/System.out(1193): Start date :: yyyy-mm-dd hh:mm:ss 2011-9-19 21:20
10-06 22:05:20.752: INFO/System.out(1193): End date :: Wed Oct 19 21:25:00 America/Tijuana 2011

EDIT

I have time in milliseconds now I am displaying that milliseconds in date format in my application but issue is when I changed timezone in device I get date according to that timezone.

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

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

发布评论

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

评论(3

江南月 2024-12-15 18:08:27

日期只是通用时间中的一个瞬间。它没有时区的概念。将其视为“肯尼迪被杀时”。

当您使用 SimpleDateFormat 显示该时间时,该日期格式具有时区,并且您使用该时区显示该时刻。因此,如果日期格式的时区是中央标准时间,则该时间将显示为 12:30。如果时区是 UTC,则显示为 18:30。

如果您希望某个日期以相同的方式显示,而不管时区如何,那么您只需选择一个特定的时区(例如 UTC),并始终显示该时区的日期。因此,在格式化日期之前,您必须对 DateFormat 调用 setTimeZone

A Date is just an instant in universal time. It has no notion of a time zone. Think of it as "when Kennedy was killed".

When you display this time with a SimpleDateFormat, this date format has a time zone, and you display this instant in time using this time zone. So, if the time zone of the date format is the central standard time, this time will be displayed as 12:30. If the time zone is UTC, it will be displayed as 18:30.

If you want some date to be displayed the same way, regardless of the time zone, than you just have to choose a specific time zone (UTC, for example), and always display the date with this time zone. You thus have to call setTimeZone on the DateFormat before formatting the date.

心碎无痕… 2024-12-15 18:08:27

在标准 Java 中:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
sdf.setTimeZone(TimeZone.getTimeZone(<your timezone here>);

long millis = <your timestamp here>; //timestamps are not TZ dependent.
Date d = new Date(millis);
String toDisplay = sdf.format(d);

SimpleDateFormat 中的时区是您要用于每个显示日期的固定时区,而不是设备的默认时区。
还要确保数据库中的时间戳列在与 Java 相同的时间点开始计数(1/1/1970,AKA Unix 纪元),并且具有相同的精度(毫秒)。

还有另一个 android.text.format.DateFormat 类,但我没有看到使用它的任何意义。

In standard Java:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
sdf.setTimeZone(TimeZone.getTimeZone(<your timezone here>);

long millis = <your timestamp here>; //timestamps are not TZ dependent.
Date d = new Date(millis);
String toDisplay = sdf.format(d);

The timezone in the SimpleDateFormat is the fixed timezone you want to use for every displayed date, not the device's default one.
Also ensure that the timestamp column in your database starts counting at the same point in time than Java (1/1/1970, AKA Unix epoch), and has the same precission (milliseconds).

There's another android.text.format.DateFormat class, but I don't see any point on using it.

在巴黎塔顶看东京樱花 2024-12-15 18:08:27

我得到如下方法的解决方案

/**
     * Date time must be in milliseconds  
     * return Time in milliseconds
     */
    public static String getTimeInMilli(Context ctx, String datetime) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(Long.parseLong(datetime));
        //This will return daylight savings offset in milliseconds.You have to Subtract  this milliseconds from calendar
        int dayLightSaving  = cal.get(Calendar.DST_OFFSET);

        cal.setTimeInMillis(cal.getTimeInMillis() - dayLightSaving);

        TimeZone z = cal.getTimeZone();
        int offset = z.getRawOffset();
        int offsetHrs = offset / 1000 / 60 / 60;
        int offsetMins = offset / 1000 / 60 % 60;

        // Subtract offset of your current TimeZone
        cal.add(Calendar.HOUR_OF_DAY, (-offsetHrs));
        cal.add(Calendar.MINUTE, (-offsetMins));

        cal.setTimeZone(TimeZone.getTimeZone("GMT"));

        //add offset of your TimeZone
        cal.add(Calendar.HOUR_OF_DAY, 5);
        cal.add(Calendar.MINUTE, 30);

        return String.valueOf(cal.getTimeInMillis());
    }

现在,如果我更改设备中的时区,我的应用程序的日期将保持不变。

I get the solution like below method

/**
     * Date time must be in milliseconds  
     * return Time in milliseconds
     */
    public static String getTimeInMilli(Context ctx, String datetime) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(Long.parseLong(datetime));
        //This will return daylight savings offset in milliseconds.You have to Subtract  this milliseconds from calendar
        int dayLightSaving  = cal.get(Calendar.DST_OFFSET);

        cal.setTimeInMillis(cal.getTimeInMillis() - dayLightSaving);

        TimeZone z = cal.getTimeZone();
        int offset = z.getRawOffset();
        int offsetHrs = offset / 1000 / 60 / 60;
        int offsetMins = offset / 1000 / 60 % 60;

        // Subtract offset of your current TimeZone
        cal.add(Calendar.HOUR_OF_DAY, (-offsetHrs));
        cal.add(Calendar.MINUTE, (-offsetMins));

        cal.setTimeZone(TimeZone.getTimeZone("GMT"));

        //add offset of your TimeZone
        cal.add(Calendar.HOUR_OF_DAY, 5);
        cal.add(Calendar.MINUTE, 30);

        return String.valueOf(cal.getTimeInMillis());
    }

Now if I change timezone in my device my application's date will remains same.

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