时间加减月数/年数

/**
* 时间加减月数
* @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();
}
posted @ 2021-08-10 16:04  游走人间的蒲公英  阅读(65)  评论(0编辑  收藏  举报