/*
* date-1 昨天
* date-2 前天
* date-3 过去3天
* date-4 过去4天
* date-5 过去5天
* date-6 过去6天
* date-29 过去一个月
* date-365 去年今天
* date1 明天
* date2 后天
* date3 未来第3天
* date4 未来第4天
* date5 未来第5天
* date6 未来第6天
* date7 未来第7天
* date30 未来一个月
* date365 明年今天
* =========== 获取方法==================
* map.get("date365") 明年今天 2023-05-03 00:00:00
* map.get("date-365") 去年今天 2023-05-03 00:00:00
* */
public static Map timesArea(){
Integer arr[] = {-1,-2,-3,-4,-5,-6,-29,-365,0,1,2,3,4,5,6,7,30,365};
HashMap<String,String> map=new HashMap<>();
for (int i = 0; i < arr.length; i++) {
Calendar calendar = Calendar.getInstance();
//如果是 + x 代表获取的是未来的日期,相反之 - x 则是获取过去的日期
calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + arr[i]);
Date date = calendar.getTime();
/*设置日期格式*/
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");//此处可以自定义格式
/*将Data转为String*/
String today = format.format(date)+" 00:00:00";//此处00:00:00可以去掉
String keyName="date"+arr[i].toString();
/*输出*/
map.put(keyName,today);
System.out.println(keyName);
System.out.println(today);
}
return map;
}