I want to get the day for the date. When i set the date in cal.set(year,month-1,day), Sets the values for the calendar fields YEAR, MONTH, DAY_OF_MONTH, HOUR_OF_DAY, and MINUTE. Previous values of other fields are retained. Thus, when I do cal.DAY_OF_WEEK, I will get the values which were existing already. But how does the cal.get(cal.DAY_OF_WEEK) gives the correct value? (As mentioned in java docs, public int get(int field) Returns the value of the given calendar field.) I don't understand how passing the integer value in this function (as calendar field that too un-modified) gives the correct output.
public static String findDay() {
Calendar cal = Calendar.getInstance();
cal.set(2019,1,15);
System.out.println(cal.toString());
System.out.println(cal.DAY_OF_WEEK); //7 which is unchanged by cal.set
System.out.println(cal.get(cal.DAY_OF_WEEK)); //6
return "";
}
Code output:
java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Etc/UTC",offset=0,dstSavings=0,useDaylight=false,transi
tions=0,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2019,MONTH=1,WEEK_OF_YEAR=7,WEEK_OF_MONTH=3,DAY_OF_MONTH=15,DAY_OF_YEAR=46,DAY_OF_WEEK=6,DAY_OF_WEEK_IN
_MONTH=3,AM_PM=0,HOUR=11,HOUR_OF_DAY=11,MINUTE=38,SECOND=14,MILLISECOND=374,ZONE_OFFSET=0,DST_OFFSET=0]
7
6