获取前七天的日期转成指定的格式,这里转成的是月/日 的格式
获取七天之前的数据 如当前12/6日 得到的数据是 12/5 12/4 12/3 12/2 12/1 11/30 11/29
public static List<String> getBefore1to7DaysEveryDay(String beginTime, String endTime) throws ParseException {
List<String> result = Lists.newArrayList();
if (StringUtils.isEmpty(beginTime) || StringUtils.isEmpty(endTime)) {
return result;
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date beginDate = sdf.parse(beginTime);
String[] s = endTime.split(" ");
endTime = s[0] + " 00:00:00";
Date endDate = sdf.parse(endTime);
Calendar calBegin = Calendar.getInstance();
calBegin.setTime(beginDate);
result.add((calBegin.get(Calendar.MONTH) + 1) + "/" + calBegin.get(Calendar.DAY_OF_MONTH));
Calendar calEnd = Calendar.getInstance();
calEnd.setTime(endDate);
while (endDate.after(calBegin.getTime())) {
calBegin.add(Calendar.DAY_OF_MONTH, 1);
result.add((calBegin.get(Calendar.MONTH) + 1) + "/" + calBegin.get(Calendar.DAY_OF_MONTH));
}
return result;
}
定义一个mapList 是所查询的天数的数组
List<String> seven7days = mapList.stream()
.filter(s -> before1to7DaysEveryDay.contains(s))
.collect(Collectors.toList());
最后得到的seven7days是所获取的每天的数据
如果要得到天数
可以用 seven7days.size();获取
外加一个格式转换为月/日,按日历来
private Map<String, List<Test>> timeFormat(List<Test> action) {
Calendar calBegin = Calendar.getInstance();
Map<String, List<Test>> collect = action.stream().collect((Collectors.groupingBy(b -> {
calBegin.setTime(b.getTodayTime());
return (calBegin.get(Calendar.MONTH) + 1) + "/" + calBegin.get(Calendar.DAY_OF_MONTH);
})));
return collect;
}