java - GregorianCalendar year setting incorrectly -


i new java, , trying figure out oddity gregoriancalendar. seems year being set incorrectly (sometimes.)

i have made following test function, illustrate problem:

public static void testtime(calendar c) {    simpledateformat sdf =           new simpledateformat("yyyy/mm/dd hh:mm:ss");           sdf.settimezone(timezone.gettimezone("gmt+0"));     system.out.println("---------------------------------");    c.settimeinmillis(0);    date d = c.gettime();    system.out.println("time 0: "  + sdf.format(d) +           " (" + d.gettime() + ")");     c.set(calendar.year, 2000);    d = c.gettime();    system.out.println("year 2000: "  + sdf.format(d) +           " (" + d.gettime() + ")");    system.out.println("---------------------------------"); } 

this function takes calendar object, , produces output should following:

--------------------------------- time 0: 1970/01/01 00:00:00 (0) year 2000: 2000/01/01 00:00:00 (946684800000) --------------------------------- 

i calling function 3 times, main; code call looks like:

   calendar c = gregoriancalendar.getinstance();    testtime(c);     c.settimezone(timezone.gettimezone("gmt+0"));    testtime(c);     c.settimezone(timezone.gettimezone("america/phoenix"));    testtime(c); 

and finally, here output:

--------------------------------- time 0: 1970/01/01 00:00:00 (0) year 2000: 2001/01/01 00:00:00 (978307200000) --------------------------------- --------------------------------- time 0: 1970/01/01 00:00:00 (0) year 2000: 2000/01/01 00:00:00 (946684800000) --------------------------------- --------------------------------- time 0: 1970/01/01 00:00:00 (0) year 2000: 2001/01/01 00:00:00 (978307200000) --------------------------------- 

as can see, when call function using gregoriancalendar obtained getinstance(), or 1 has had time zone set "america/phoenix" (my local time zone), the year output 2001. when set time zone gmt, year (correctly believe) output 2000.

can please explain me missing? use calendar creating one, setting fields using variables have, , generating date. however, need make sure date getting accurately reflect setting; going crazy unit tests until realized getting dates didn't match year entering, made above code test.

edit: clear, not concerned small offset due timezone. can understand, , account for. time difference of +-24 hours due time zone 1 thing; time difference of year (2001 instead of 2000) quite another.

if matters, java running on 64bit linux machine; java -showversion command returns:

java version "1.8.0_40" java(tm) se runtime environment (build 1.8.0_40-b26) java hotspot(tm) 64-bit server vm (build 25.40-b25, mixed mode) 

thanks in advance help. have searched boards here, , internet as can, , can't find comes close problem.

ok, figured out going on.

when set year, indeed set year 2000. seems update rest of internal fields, using time zone has.

this means when set year 2000, sets field, , computes time (including time zone). previous time jan 1 00:00:00 1970. updates year 2000, moves time 7 hours (my offset) gives time of dec 31 17:00 mst. year should roll 1999, leaves year @ 2000, since set to. resulting date therefore dec 31 17:00 2000.

when print date out, set time zone of simpledateformat gmt. therefore moves time forward 7 hours, jan 1 2001 00:00:00.

that confusing, repeated tests show indeed doing way. strange, true. lesson here (for me) should set time zone of output object match timezone of calendar. show dates match input.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -