Java: Timezone why different timezone give same value in millisec

前端 未结 3 1662
清歌不尽
清歌不尽 2020-12-03 15:05

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         


        
相关标签:
3条回答
  • 2020-12-03 15:06

    Why different Timezone give SAME value in millisec?

    Because that's what it's meant to do. From the documentation:

    (Returns) the current time as UTC milliseconds from the epoch.

    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.

    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.

    0 讨论(0)
  • 2020-12-03 15:19

    The millisec of a Date object in Java is just the milliseconds since GMT+0 1970/01/01 00:00:00. It's independent of the Time Zone. Time Zone is a property to format the Date to a readable string.

    0 讨论(0)
  • 2020-12-03 15:29

    getTimeInMillis() method return the current time as UTC milliseconds from the epoch. So, you are getting same milliseconds even both calendar object has different timezone.

    0 讨论(0)
提交回复
热议问题