月:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
logger.debug("当前时间是:" + dateFormat.format(date));
Calendar calendar = Calendar.getInstance();
calendar.setTime(date); // 设置为当前时间
calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1); // 设置为上一个月 +为后一个月 0 为本月
date = calendar.getTime();
logger.debug("上一个月的时间: " + dateFormat.format(date));
天:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
// 定义数组
String[]time1=new String[7];
for(int i=6;i>=0;i--){
Date date = new Date();
logger.debug("当前时间是:" + dateFormat.format(date));
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - i); // 设置为前i天
date = calendar.getTime();
//将获取的日期添加的数组
time1[6-i]=dateFormat.format(date);
}
logger.debug("钱一周时间:{}",Arrays.toString(time1));