有人请解释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和MINUTE 的值.保留其他字段的先前值.因此,当我执行cal.DAY_OF_WEEK时,我将获得已经存在的值.但是,cal.get(cal.DAY_OF_WEEK)如何给出正确的值?(如java docs中所述, public int get(int field)返回给定日历字段的值.)我不明白如何在此函数中传递整数值(作为日历)字段而未修改)给出正确的输出.
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 "";
}
代码输出:
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
推荐答案
我们来剖析这些内容:
此:
System.out.println(cal.toString());
为您的Calendar对象打印 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,transitions = 0,lastRule = null],firstDayOfWeek = 1,minimumDaysInFirstWeek = 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]
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
And yes, here DAY_OF_WEEK is 6
与此同时:
System.out.println(cal.DAY_OF_WEEK); //7 which is unchanged by cal.set
返回 Calendar.DAY_OF_WEEK
字段结果,该结果为常量,==等于7.
returns the Calendar.DAY_OF_WEEK
field result, which is a constant and is == to 7.
最后这个:
System.out.println(cal.get(cal.DAY_OF_WEEK)); //6
返回6,这是您的Calendar对象保留的DAY_OF_WEEK字段.
returns 6, which is the DAY_OF_WEEK field held by your Calendar object.
将您的Calendar对象(简化)生动描述如下:
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 + .....;
}
}
现在有意义吗?您正在设置一个隐藏的dayOfWeek字段并获取
make sense now? There is a hidden dayOfWeek field that you're setting and getting the result of
所以这个:
System.out.println(cal.get(cal.DAY_OF_WEEK));
实际上返回 dayOfWeek
私有字段结果
actually returns the dayOfWeek
private field result
这篇关于有人请解释Calendar.get(cal.DAY_OF_WEEK)如何提供正确的输出,而cal.DAY_OF_WEEK是默认值且未修改吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!