Java:时区为什么不同的时区以毫秒为单位给出相同的值 [英] Java: Timezone why different timezone give same value in millisec
问题描述
我有以下代码,我的目标是以毫秒为单位返回 GMT + 0
时间。但为什么我总是得到我当地的时区millisec?
I have following code, my target is going to return GMT+0
time in millisec. But Why I always get my local timezone millisec?
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
Calendar cal2 = Calendar.getInstance();
System.out.println("Time zone id is:"+cal.getTimeZone().getID()+";time in millisec:"+cal.getTimeInMillis());
System.out.println("Time zone id is:"+cal2.getTimeZone().getID()+";time in millisec:"+cal2.getTimeInMillis());
输出
时区ID是:GMT;时间以毫秒为单位: 1332740915154
时区ID为:欧洲/赫尔辛基;时间以毫秒为单位: 1332740915154
为什么不同的时区以毫秒为单位给出SAME值?
我想如果它是 GMT + 0
然后它应该是毫秒与本地时区的不同值。
Why different Timezone give SAME value in millisec?
I suppose if it is GMT+0
then it should be different value in millisec against local time zone.
推荐答案
为什么不同的时区以毫秒为单位给出SAME值?
Why different Timezone give SAME value in millisec?
因为这就是的意思。从文档中:
Because that's what it's meant to do. From the documentation:
(返回)从纪元开始的UTC毫秒当前时间。
(Returns) the current time as UTC milliseconds from the epoch.
换句话说,它是 getTime $返回的
Date
中的值c $ c> - 它不依赖于时区。如果您想要取决于时区的值,请使用 Calendar.Get(Calendar.YEAR)
等。
In other words, it's the value which would be in the Date
returned by getTime
- it doesn't depend on the time zone. If you want values which depend on the time zone, use Calendar.Get(Calendar.YEAR)
etc.
Calendar.getTime()
和 Calendar.getTimeInMillis()
返回表示 instant 的值在日历内的时间,它独立于时区和日历系统。
Both Calendar.getTime()
and Calendar.getTimeInMillis()
return values representing the instant in time within the calendar, which is independent of both time zone and calendar system.
这篇关于Java:时区为什么不同的时区以毫秒为单位给出相同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!