这是东西:
http://www.javalaunch.com/Java-Date-Calender-time-2.html
任何人都可以告诉我这个Calendar.YEAR和calender.get(Calendar.YEAR)背后的逻辑是什么。实际上我不明白这一点。
System.out.println("Year: " + Calendar.YEAR);
System.out.println("month: " + Calendar.MONTH);
System.out.println("dayOfMonth: " + Calendar.DAY_OF_MONTH); // Jan = 0, not 1
System.out.println("dayOfWeek: " + Calendar.DAY_OF_WEEK);
System.out.println("weekOfYear: " + Calendar.WEEK_OF_YEAR);
System.out.println("weekOfMonth: " + Calendar.WEEK_OF_MONTH);
System.out.println("hour: " + Calendar.HOUR); // 12 hour clock
System.out.println("hourOfDay: " + Calendar.HOUR_OF_DAY); // 24 hour clock
System.out.println("minute: " + Calendar.MINUTE);
System.out.println("second: " + Calendar.SECOND);
System.out.println("millisecond: " + Calendar.MILLISECOND);
System.out.println("...................................................................................");
Calendar calender = new GregorianCalendar();
System.out.println("Year: " + calender.get(Calendar.YEAR));
System.out.println("month: " + calender.get(Calendar.MONTH));
System.out.println("dayOfMonth: " + calender.get(Calendar.DAY_OF_MONTH)); // Jan = 0, not 1
System.out.println("dayOfWeek: " + calender.get(Calendar.DAY_OF_WEEK));
System.out.println("weekOfYear: " + calender.get(Calendar.WEEK_OF_YEAR));
System.out.println("weekOfMonth: " + calender.get(Calendar.WEEK_OF_MONTH));
System.out.println("hour: " + calender.get(Calendar.HOUR)); // 12 hour clock
System.out.println("hourOfDay: " + calender.get(Calendar.HOUR_OF_DAY)); // 24 hour clock
System.out.println("minute: " + calender.get(Calendar.MINUTE));
System.out.println("second: " + calender.get(Calendar.SECOND));
System.out.println("millisecond: " + calender.get(Calendar.MILLISECOND));
输出:
Year: 1
month: 2
dayOfMonth: 5
dayOfWeek: 7
weekOfYear: 3
weekOfMonth: 4
hour: 10
hourOfDay: 11
minute: 12
second: 13
millisecond: 14
...................................................................................
Year: 2014
month: 11
dayOfMonth: 24
dayOfWeek: 4
weekOfYear: 52
weekOfMonth: 4
hour: 11
hourOfDay: 11
minute: 51
second: 54
millisecond: 687
答案 0 :(得分:3)
当您使用(Calendar.YEAR, Calendar.MONTH),
这些是Calendar class
中的静态字段时,如果您要打印这些字段,则会打印这些字段的默认值。
如果是
Calendar calendar = new GregorianCalendar();
这将使用当前时间和日期值创建一个新的日历实例,并且您正在使用这些静态最终变量从新创建的日历实例中获取该特定值。
您可以在此处查看Calender
的文档<强>更新强>
下面是get方法的代码,当你传递这些静态字段来获取方法时,它将从该索引的fields数组返回该字段的值。
public int get(int field)
{
complete();
return internalGet(field);
}
protected final int internalGet(int field)
{
return fields[field];
}