日历getTimeInMillis()是时区依赖吗? [英] Calendar getTimeInMillis() is timezone-dependent?
问题描述
指向上面的链接我假设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屋!