有人请解释 Calendar.get(cal.DAY_OF_WEEK) 如何给出正确的输出,而 cal.DAY_OF_WEEK 是默认的且未修改?

Someone please explain how Calendar.get(cal.DAY_OF_WEEK) gives correct output while cal.DAY_OF_WEEK is default and not modified?

我想知道约会的日期。当我在 cal.set(year,month-1,day) 中设置日期时,设置日历字段的值 YEAR、MONTH、DAY_OF_MONTH、HOUR_OF_DAY 和分钟。保留其他字段的先前值。因此,当我执行 cal.DAY_OF_WEEK 时,我将获得已经存在的值。 但是 cal.get(cal.DAY_OF_WEEK) 是如何给出正确值的呢? (如 java 文档中所述,public int get(int field) Returns 给定日历字段的值。)我不了解如何在此函数中传递整数值 (作为未修改的日历字段) 给出正确的输出。

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 "";

}

代码输出:

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

如果您查看 Calendar class 的 java 文档,它清楚地提到了 cal.DAY_OF_WEEK 的用途。它只代表一个字段的编号,get() 方法接受字段编号和 returns 它的值。您可以将 cal.DAY_OF_WEEK 的值视为数组的索引,get() 方法从中检索值。请参阅下面 get() 方法的文档。

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

让我们来剖析一下:

这个:

System.out.println(cal.toString());

打印出您的日历对象的 toString()、其字段的状态,以及 returns 这个:

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]

是的,这里 DAY_OF_WEEK 是 6

同时:

System.out.println(cal.DAY_OF_WEEK); //7 which is unchanged by cal.set

returns Calendar.DAY_OF_WEEK 字段结果,它是一个常量,== 到 7。

最后这个:

System.out.println(cal.get(cal.DAY_OF_WEEK)); //6

returns 6,这是您的日历对象持有的 DAY_OF_WEEK 字段。

将您的日历对象(简化版)想象成这样:

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 + .....;
    }
}

现在明白了吗?您正在设置一个隐藏的 dayOfWeek 字段并获得

的结果

所以这个:

System.out.println(cal.get(cal.DAY_OF_WEEK));

实际上returns dayOfWeek 私有字段结果