java 获取当前时间前后n天的日期
/**
* 获取当前时间往前往后n的日期
* eg: date ==null 默认是系统当前时间 否则以date为时间起点
* n > 0 往后
* n = 0 当前时间
* n < 0 往前
* @author zhangheng5@lenovo.com
* @param date
* @param n
* @return
*/
public static List<String> getBeforeDate(Date date,int n){
List<String> list = new ArrayList<>();
Calendar c = Calendar.getInstance();
Date today = new Date();
if(null != date){
today = date;
}
c.setTime(today);
if(n > 0){
for(int i=0;i<=n;i++){
c.add(Calendar.DATE, i);
list.add(new SimpleDateFormat("yyyy-MM-dd").format(c.getTime()));
c.setTime(today);
}
}else if (n < 0){
for(int i=n;i<=0;i++){
c.add(Calendar.DATE, i);
list.add(new SimpleDateFormat("yyyy-MM-dd").format(c.getTime()));
c.setTime(today);
}
}else{
c.add(Calendar.DATE, 0);
list.add(new SimpleDateFormat("yyyy-MM-dd").format(c.getTime()));
c.setTime(today);
}
return list;
}