Confusion with Java Calendar and time zones -


consider following code fragment...

import java.util.calendar; import java.util.timezone;   public class main {     public static void main(string[] args) {          calendar c = calendar.getinstance();         system.out.println(c.gettimezone().getdisplayname());         system.out.println(c.gettime());         system.out.println(c.get(calendar.hour_of_day));          calendar gmtcal = calendar.getinstance(timezone.gettimezone("gmt"));         system.out.println(gmtcal.gettimezone().getdisplayname());         system.out.println(gmtcal.gettime());         system.out.println(gmtcal.get(calendar.hour_of_day));          calendar c2 = calendar.getinstance(timezone.gettimezone("us/alaska"));         system.out.println(c2.gettimezone().getdisplayname());         system.out.println(c2.gettime());         system.out.println(c2.get(calendar.hour_of_day));     } } 

the output of program is

eastern standard time mon jul 13 16:10:14 edt 2015  16 greenwich mean time mon jul 13 16:10:14 edt 2015  //<--- not sure why isn't 4 hours ahead eastern time utc/gmt - 4 right due dst 20 alaska standard time mon jul 13 16:10:14 edt 2015 //<--- date not reflecting correct time , showing edt versus ast 12 

why get(calendar.hour_of_day) method call not match hour in gettime() method call? way put it, why isn't output?

eastern standard time mon jul 13 16:10:14 edt 2015 16 greenwich mean time mon jul 13 20:10:14 gmt 2015 20 alaska standard time mon jul 13 12:10:14 ast 2015 12 

edit... how can following

long t = 1436842840327l; calendar c = calendar.getinstance(); c.settimeinmillis(t); c.settimezone(timezone.gettimezone("us/alaska")); system.out.println(c.gettime()); system.out.println(c.get(calendar.hour_of_day) + ":" + c.get(calendar.minute) + ":" + c.get(calendar.millisecond)); 

to print same hour in gettime()? output is

mon jul 13 23:00:40 edt 2015 19:0:327 

the function gettime() returns date object, , date objects, when when converted string, represented using default time zone.

so using gettime() you're getting date object no longer contains timezone data (a date specific point in time). date object gets implicitly converted string when it's printed.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -