时间加减月数/年数

/**
* 时间加减月数
* @param startDate 要处理的时间,Null则为当前时间
* @param months 加减的月数
* @return Date
*/
public static Date dateAddMonths(Date startDate, int months) {
if (startDate == null) {
startDate = new Date();
}
Calendar c = Calendar.getInstance();
c.setTime(startDate);
c.set(Calendar.MONTH, c.get(Calendar.MONTH) + months);
return c.getTime();
}



/**
* 时间加减年数
* @param startDate 要处理的时间,Null则为当前时间
* @param years 加减的年数
* @return Date
*/
public static Date dateAddYears(Date startDate, int years) {
if (startDate == null) {
startDate = new Date();
}
Calendar c = Calendar.getInstance();
c.setTime(startDate);
c.set(Calendar.YEAR, c.get(Calendar.YEAR) + years);
return c.getTime();
}
原文地址:https://www.cnblogs.com/gnpugongying/p/15124316.html