abstract public void add(int field, int amount);
两个参数,第一个参数表示类型,总共1-14;1代表的是对年份操作,2
是对月份
操作,3是对星期操作,5
是对日期
操作,11是对小时操作,12
是对分钟
操作,13是对秒操作,14
是对毫秒
操作。
第二个参数则是加或者减指定的 年
/月
/周
/日
/时
/分
/秒
/毫秒
例子:
Calendar calendar = Calendar.getInstance();
calendar.add(5,1);
//则表示对日期进行加一天操作
//测试:
Calendar cal = Calendar.getInstance();
cal.add(5,-1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
String day = sdf.format(cal.getTime());
System.out.println(day);
源码:
public void add(int field, int amount) {
// If amount == 0, do nothing even the given field is out of
// range. This is tested by JCK.
if (amount == 0) {
return; // Do nothing!
}
if (field < 0 || field >= ZONE_OFFSET) {//ZONE_OFFSET==15
throw new IllegalArgumentException();
}
// Sync the time and calendar fields.
complete();
if (field == YEAR) {//YEAR==1
int year = internalGet(YEAR);
if (internalGetEra() == CE) {
year += amount;
if (year > 0) {
set(YEAR, year);
} else { // year <= 0
set(YEAR, 1 - year);
// if year == 0, you get 1 BCE.
set(ERA, BCE);
}
}
else { // era == BCE
year -= amount;
if (year > 0) {
set(YEAR, year);
} else { // year <= 0
set(YEAR, 1 - year);
// if year == 0, you get 1 CE
set(ERA, CE);
}
}
pinDayOfMonth();
} else if (field == MONTH) {
int month = internalGet(MONTH) + amount;
int year = internalGet(YEAR);
int y_amount;
if (month >= 0) {
y_amount = month/12;
} else {
y_amount = (month+1)/12 - 1;
}
if (y_amount != 0) {
if (internalGetEra() == CE) {
year += y_amount;
if (year > 0) {
set(YEAR, year);
} else { // year <= 0
set(YEAR, 1 - year);
// if year == 0, you get 1 BCE
set(ERA, BCE);
}
}
else { // era == BCE
year -= y_amount;
if (year > 0) {
set(YEAR, year);
} else { // year <= 0
set(YEAR, 1 - year);
// if year == 0, you get 1 CE
set(ERA, CE);
}
}
}
if (month >= 0) {
set(MONTH, month % 12);
} else {
// month < 0
month %= 12;
if (month < 0) {
month += 12;
}
set(MONTH, JANUARY + month);
}
pinDayOfMonth();
} else if (field == ERA) {
int era = internalGet(ERA) + amount;
if (era < 0) {
era = 0;
}
if (era > 1) {
era = 1;
}
set(ERA, era);
} else {
long delta = amount;
long timeOfDay = 0;
switch (field) {
// Handle the time fields here. Convert the given
// amount to milliseconds and call setTimeInMillis.
case HOUR:
case HOUR_OF_DAY:
delta *= 60 * 60 * 1000; // hours to minutes
break;
case MINUTE:
delta *= 60 * 1000; // minutes to seconds
break;
case SECOND:
delta *= 1000; // seconds to milliseconds
break;
case MILLISECOND:
break;
// Handle week, day and AM_PM fields which involves
// time zone offset change adjustment. Convert the
// given amount to the number of days.
case WEEK_OF_YEAR:
case WEEK_OF_MONTH:
case DAY_OF_WEEK_IN_MONTH:
delta *= 7;
break;
case DAY_OF_MONTH: // synonym of DATE
case DAY_OF_YEAR:
case DAY_OF_WEEK:
break;
case AM_PM:
// Convert the amount to the number of days (delta)
// and +12 or -12 hours (timeOfDay).
delta = amount / 2;
timeOfDay = 12 * (amount % 2);
break;
}
// The time fields don't require time zone offset change
// adjustment.
if (field >= HOUR) {
setTimeInMillis(time + delta);
return;
}
// The rest of the fields (week, day or AM_PM fields)
// require time zone offset (both GMT and DST) change
// adjustment.
// Translate the current time to the fixed date and time
// of the day.
long fd = getCurrentFixedDate();
timeOfDay += internalGet(HOUR_OF_DAY);
timeOfDay *= 60;
timeOfDay += internalGet(MINUTE);
timeOfDay *= 60;
timeOfDay += internalGet(SECOND);
timeOfDay *= 1000;
timeOfDay += internalGet(MILLISECOND);
if (timeOfDay >= ONE_DAY) {
fd++;
timeOfDay -= ONE_DAY;
} else if (timeOfDay < 0) {
fd--;
timeOfDay += ONE_DAY;
}
fd += delta; // fd is the expected fixed date after the calculation
int zoneOffset = internalGet(ZONE_OFFSET) + internalGet(DST_OFFSET);
setTimeInMillis((fd - EPOCH_OFFSET) * ONE_DAY + timeOfDay - zoneOffset);
zoneOffset -= internalGet(ZONE_OFFSET) + internalGet(DST_OFFSET);
// If the time zone offset has changed, then adjust the difference.
if (zoneOffset != 0) {
setTimeInMillis(time + zoneOffset);
long fd2 = getCurrentFixedDate();
// If the adjustment has changed the date, then take
// the previous one.
if (fd2 != fd) {
setTimeInMillis(time - zoneOffset);
}
}
}
}