日历getTimeInMillis()是时区依赖吗? [英] Calendar getTimeInMillis() is timezone-dependent?

查看:812
本文介绍了日历getTimeInMillis()是时区依赖吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java:时区,为什么不同的时区给出相同的值以毫秒为单位



指向上面的链接我假设Calendar类的getTimeInMillis()返回独立于时区的毫秒数。



但使用以下代码:

  Calendar cal = Calendar.getInstance 
int dateInSeconds =(int)(cal.getTimeInMillis()/ 1000);
Log.i(SECONDS TIME:,+ dateInSeconds);

尝试,首先,将我的系统时钟设置为10:00和GMT + 02:00



产生一定的输出数。但将系统时钟设置为10:00和GMT + 00:00



输出数字大约比前一种情况大7200,这相当于大约2小时。 p>

为什么?

解决方案

获取独立于时区的毫秒数我找到以下解决方案



而不是:

  int dateInSeconds = (int)(cal.getTimeInMillis()/ 1000); 

我使用:

  int dateInSeconds =(int)((cal.getTimeInMillis()+ cal.getTimeZone()。getOffset(cal.getTimeInMillis()))/ 1000); 

它对我有用。


Java: Timezone why different timezone give same value in millisec

referring to the above link I have supposed that getTimeInMillis() of Calendar class returns the number of milliseconds independently from time zone.

But using the following code:

 Calendar cal = Calendar.getInstance();
 int dateInSeconds = (int)(cal.getTimeInMillis()/1000);
 Log.i("TIME IN SECONDS: ",""+dateInSeconds);

tried,at first, to set my system clock to 10:00 and GMT+02:00

producing a certain output number. But setting system clock to 10:00 and GMT+00:00

the output number is about 7200 greater than the prior case, which correspons to about 2 hours.

Why?

解决方案

For obtaining the number of milliseconds independent to time zone I found the following solution

Instead of:

 int dateInSeconds = (int)(cal.getTimeInMillis()/1000);

I use:

 int dateInSeconds = (int)((cal.getTimeInMillis()+cal.getTimeZone().getOffset(cal.getTimeInMillis()))/1000);

it works good for me.

这篇关于日历getTimeInMillis()是时区依赖吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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