java - Calendar not giving correct date -


i'll using mm/dd/yyyy format below code should give me "07/01/2017" instead giving me "06/30/2017" if uncomment //cal.gettime(); i'm getting "07/01/2017"

can explain behaviour.

public static void main(string[] args) {     calendar cal = calendar.getinstance();     date date = new date("04/02/2017");     cal.settime(date);     system.out.println(date);     calendar nqtstartdate = new gregoriancalendar(cal.get(calendar.year),              calendar.june, 30);     cal = nqtstartdate;     //cal.gettime();      cal.set(calendar.day_of_week, calendar.saturday);       system.out.println(cal.gettime());  } 

when call gettime() method internally update time , change state of variable istimeset (which, see reading source code, makes class not thread safe).

public long gettimeinmillis() {     if (!istimeset) {         updatetime();     }     return time; } 

on other hand statement cal.set(calendar.day_of_week, calendar.saturday) reset state of internal time (changing variable istimeset false).

the state of variable istimeset true if value of internal time valid.

just summarise: not change calendar configuration (i.e. set day of week) after have called gettime() method because reset internal status of calendar.


Comments