1则代表的是对年份操作,2是对月份操作,3是对星期操作,5是对日期操作,11是对小时操作,12是对分钟操作,13是对秒操作,14是对毫秒操作。
public static Date addYears(Date date, int amount) { return add(date, 1, amount); } public static Date addMonths(Date date, int amount) { return add(date, 2, amount); } public static Date addWeeks(Date date, int amount) { return add(date, 3, amount); } public static Date addDays(Date date, int amount) { return add(date, 5, amount); } public static Date addHours(Date date, int amount) { return add(date, 11, amount); } public static Date addMinutes(Date date, int amount) { return add(date, 12, amount); } public static Date addSeconds(Date date, int amount) { return add(date, 13, amount); } public static Date addMilliseconds(Date date, int amount) { return add(date, 14, amount); } private static Date add(Date date, int calendarField, int amount) { if(date == null) { throw new IllegalArgumentException("The date must not be null"); } else { Calendar c = Calendar.getInstance(); c.setTime(date); c.add(calendarField, amount); return c.getTime(); }