//String sfjj 是否加减 0 减, 1 是 加。
//String type (year-年 ,month-月 ,day-日)
//int num 是加减数
public static String addOrsubtractByDate(String date ,String type ,String sfjj ,int num) throws ParseException {
//将String类型的时间转为date类型
SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd");
Date nowDate=formatter.parse(date);
//创建Calendar对象进行时间加减
Calendar rightNow = Calendar.getInstance();
//将当天的时间注入Calendar对象
rightNow.setTime(nowDate);
if("year".equals(type)){
if("0".equals(sfjj)){
rightNow.add(Calendar.YEAR, -num);
}else{
rightNow.add(Calendar.YEAR, +num);
}
}else if("month".equals(type)){
if("0".equals(sfjj)){
rightNow.add(Calendar.MONTH, -num);
}else{
rightNow.add(Calendar.MONTH, +num);
}
}else if("day".equals(type)){
if("0".equals(sfjj)){
rightNow.add(Calendar.DATE, -num);
}else{
rightNow.add(Calendar.DATE, +num);
}
}
//获取时间
Date dt1=rightNow.getTime();
//将date类型时间转为String类型时间
String stringDate = formatter.format(dt1);
return stringDate;
}