问题
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
回答1:
Let's dissect these:
This:
System.out.println(cal.toString());
prints out the toString()
for your Calendar object, the state of its fields, and returns this:
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]
And yes, here DAY_OF_WEEK is 6
While this:
System.out.println(cal.DAY_OF_WEEK); //7 which is unchanged by cal.set
returns the Calendar.DAY_OF_WEEK
field result, which is a constant and is == to 7.
Finally this:
System.out.println(cal.get(cal.DAY_OF_WEEK)); //6
returns 6, which is the DAY_OF_WEEK field held by your Calendar object.
Picture your Calendar object (simplified) to look like this:
public abstract class Calendar {
public static int DAY_OF_WEEK = 7; // a constant that never changes
// ..... many more fields
// field name may be different
private int dayOfWeek = 0; // or some other default value
public void set(...) {
// sets the dayOfWeek value
}
public int get(int ) {
if (field == DAY_OF_WEEK) {
return this.dayOfWeek;
}
// .... more code
}
public String toString() {
return .... + "DAY_OF_WEEK:" + dayOfWeek + .....;
}
}
make sense now? There is a hidden dayOfWeek field that you're setting and getting the result of
So this:
System.out.println(cal.get(cal.DAY_OF_WEEK));
actually returns the dayOfWeek
private field result
回答2:
If you look into the java doc of Calendar
class, it clearly mentions what is the purpose of cal.DAY_OF_WEEK
. It just represents the number of a field and the get()
method accept the field number and returns its value. You can consider the value of cal.DAY_OF_WEEK
as an index of an array from which the get()
method retrieves the value. Please see below the documentation of get()
method.
Returns the value of the given calendar field. In lenient mode,
* all calendar fields are normalized. In non-lenient mode, all
* calendar fields are validated and this method throws an
* exception if any calendar fields have out-of-range values. The
* normalization and validation are handled by the
* {@link #complete()} method, which process is calendar
来源:https://stackoverflow.com/questions/54708960/someone-please-explain-how-calendar-getcal-day-of-week-gives-correct-output-wh