相关源码:

/*
 * ...... 
 * <h3>Getting and Setting Calendar Field Values</h3>
 *
 * <p>The calendar field values can be set by calling the <code>set</code>
 * methods. Any field values set in a <code>Calendar</code> will not be
 * interpreted until it needs to calculate its time value (milliseconds from
 * the Epoch) or values of the calendar fields. Calling the
 * <code>get</code>, <code>getTimeInMillis</code>, <code>getTime</code>,
 * <code>add</code> and <code>roll</code> involves such calculation.
 * ......
 */
public abstract class Calendar implements Serializable, Cloneable, Comparable<Calendar> {
    ......
}
  • 可以通过set方法修改Calendar实例各个Field的值;
  • 只有在真正需要使用calendar的值之前,这种改变才会生效;
  • get()、getTimeMillis()、getTime()、add()、roll()方法可以触发set方法的修改。

例子:

public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        System.out.println(calendar);

        calendar.set(Calendar.DAY_OF_MONTH, 1);
        System.out.println(calendar);

        calendar.getTime();
        System.out.println(calendar);
}
输出1 输出2 输出3
java.util.GregorianCalendar[
time=1608696480267,
areFieldsSet=true,
areAllFieldsSet=true,
lenient=true,
firstDayOfWeek=1,
minimalDaysInFirstWeek=1,
ERA=1,
YEAR=2020,
MONTH=11,
WEEK_OF_YEAR=52,
WEEK_OF_MONTH=4,
DAY_OF_MONTH=23,
DAY_OF_YEAR=358,
DAY_OF_WEEK=4,
DAY_OF_WEEK_IN_MONTH=4,
AM_PM=1,
HOUR=0,
HOUR_OF_DAY=12,
MINUTE=8,
SECOND=0,
MILLISECOND=267,
ZONE_OFFSET=28800000,
DST_OFFSET=0]
java.util.GregorianCalendar[
time=?,
areFieldsSet=false,
areAllFieldsSet=true,
lenient=true,
firstDayOfWeek=1,
minimalDaysInFirstWeek=1,
ERA=1,
YEAR=2020,
MONTH=11,
WEEK_OF_YEAR=52,
WEEK_OF_MONTH=4,
DAY_OF_MONTH=1,
DAY_OF_YEAR=358,
DAY_OF_WEEK=4,
DAY_OF_WEEK_IN_MONTH=4,
AM_PM=1,HOUR=0,
HOUR_OF_DAY=12,
MINUTE=8,
SECOND=0,
MILLISECOND=267,
ZONE_OFFSET=28800000,
DST_OFFSET=0]
java.util.GregorianCalendar[
time=1606795680267,
areFieldsSet=true,
areAllFieldsSet=false,
lenient=true,
firstDayOfWeek=1,
minimalDaysInFirstWeek=1,
ERA=1,
YEAR=2020,
MONTH=11,
WEEK_OF_YEAR=49,
WEEK_OF_MONTH=1,
DAY_OF_MONTH=1,
DAY_OF_YEAR=336,
DAY_OF_WEEK=3,
DAY_OF_WEEK_IN_MONTH=1,
AM_PM=1,
HOUR=0,
HOUR_OF_DAY=12,
MINUTE=8,
SECOND=0,
MILLISECOND=267,
ZONE_OFFSET=28800000,
DST_OFFSET=0]