查询一年中每一天的数据

查询一年中每一天的数据

当我们想要查询一年中每一天的数据应该怎么办

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//查询一年前的今天
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.YEAR, -1);
Date beferOneyear = c.getTime();
//当天时间
Date today = new Date();
// 封装开始时间和结束时间
req.setAccountingDtStart(sdf.format(beferOneyear));
req.setAccountingDtEnd(sdf.format(today));


//获取开始时间和结束时间中间的天数
LocalDate accountingDtStart = LocalDate.parse(req.getAccountingDtStart(),
DateTimeFormatter.ofPattern("yyyyMMdd"));
LocalDate accountingDtEnd = LocalDate.parse(req.getAccountingDtEnd(), DateTimeFormatter.ofPattern("yyyyMMdd"));
List<LocalDate> totalList = getMiddleDate(accountingDtStart, accountingDtEnd);
List<LocalDate> pageList = page(totalList, page.getPageNum(), page.getPageSize());
//获取总共有多少天
pageList.forEach(item -> {
// 创建业务类,根据日期天数讲业务类加到map中
DevopsSummaryQueryResp devopsSummaryQueryResp = new DevopsSummaryQueryResp();
devopsSummaryQueryResp.setAccountingDate(item.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
map.put(item.format(DateTimeFormatter.ofPattern("yyyyMMdd")), devopsSummaryQueryResp);
});


/**
* 进行分页
* @param begin
* @param end
* @return
*/
public static <T> List<T> page(List<T> list, Integer pageNum, Integer pageSize) {
if(pageNum == null && pageSize == null){
return list;
}
int fromIndex = (pageNum - 1) * pageSize;
int toIndex = pageNum * pageSize;
if (fromIndex >= list.size()) {
return Collections.emptyList();
}
if (toIndex >= list.size()) {
toIndex = list.size();
}
return list.subList(fromIndex, toIndex);
}

/**
* 获取开始时间和结束时间中间的天数
* @param begin
* @param end
* @return
*/
public static List<LocalDate> getMiddleDate(LocalDate begin, LocalDate end) {
List<LocalDate> localDateList = new ArrayList<>();
long length = end.toEpochDay() - begin.toEpochDay();
for (int i = 0; i < length + 1; i++) {
localDateList.add(end.minusDays(i));
}
return localDateList;
}