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());
The output is
Time zone id is:GMT;time in millisec:1332740915154
Time zone id is:Europe/Helsinki;time in millisec:1332740915154
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.
Because that’s what it’s meant to do. From the documentation:
In other words, it’s the value which would be in the
Datereturned bygetTime– it doesn’t depend on the time zone. If you want values which depend on the time zone, useCalendar.Get(Calendar.YEAR)etc.Both
Calendar.getTime()andCalendar.getTimeInMillis()return values representing the instant in time within the calendar, which is independent of both time zone and calendar system.