工作笔记Java日期查询查询一年中每一天的数据三生石2023-12-052023-12-05查询一年中每一天的数据当我们想要查询一年中每一天的数据应该怎么办 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263SimpleDateFormat 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; }