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
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.
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.
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.