java统计数据(本周、本月、本年)

本周

LocalDate localDate = LocalDate.now();
        LocalDate startTime = localDate.with(DayOfWeek.MONDAY);
        LocalDate endTime = localDate.with(DayOfWeek.SUNDAY);
        List<String> xList = Lists.newArrayList();
        List<Integer> yList = Lists.newArrayList();
        Map<String, Object> valueMap = new HashMap<>();
        // 获取本周的所有数据
        List<DeathDetail> list = deathDetailDAO.betwenStartAndEndTime(startTime, endTime);
        Map<LocalDate, List<DeathDetail>> map = list.stream().collect(Collectors.groupingBy(e -> e.getDeathTime()));
        for (LocalDate d = startTime; d.isBefore(endTime.plusDays(1)); d = d.plusDays(1)) {
            String weekName = d.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.CHINA);
            xList.add(weekName);
            if (!map.isEmpty()) {
                List<DeathDetail> deathDetails = map.getOrDefault(d, new ArrayList<>());
                yList.add(deathDetails.size());
            }
        }
        valueMap.put("xValue", xList);
        valueMap.put("yValue", yList);
        return new ObjectResponse<>(valueMap);

本月

LocalDate localDate = LocalDate.now();
        LocalDate startTime = localDate.with(TemporalAdjusters.firstDayOfMonth());
        LocalDate endTime = localDate.with(TemporalAdjusters.lastDayOfMonth());
        List<DeathDetail> list = deathDetailDAO.betwenStartAndEndTime(startTime, endTime);
        List<Integer> xList = Lists.newArrayList();
        List<Integer> yList = Lists.newArrayList();
        Map<Integer, List<DeathDetail>> collect = list.stream().collect(Collectors.groupingBy(o -> o.getDeathTime().getDayOfMonth()));
        for (LocalDate d = startTime; d.isBefore(endTime.plusDays(1)); d = d.plusDays(1)) {
            int dayOfMonth = d.getDayOfMonth();
            xList.add(dayOfMonth);
            if (!collect.isEmpty()) {
                List<DeathDetail> deathDetailList = collect.getOrDefault(d.getDayOfMonth(), new ArrayList<>());
                yList.add(deathDetailList.size());
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("xValue", xList);
        map.put("yValue", yList);
        return new ObjectResponse<>(map);

全年12个月

LocalDate localDate = LocalDate.now();
        LocalDate startTime = localDate.with(TemporalAdjusters.firstDayOfYear());
        LocalDate endTime = localDate.with(TemporalAdjusters.lastDayOfYear());
        List<DeathDetail> list = deathDetailDAO.betwenStartAndEndTime(startTime, endTime);
        Map<Integer, List<DeathDetail>> collect = list.stream().collect(Collectors.groupingBy(e -> e.getDeathTime().getMonthValue()));
        List<String> xList = Lists.newArrayList();
        List<Integer> yList = Lists.newArrayList();
        int month = 12;
        for (int i = 1; i <= month; i++) {
            xList.add(i + "月");
            if (!collect.isEmpty()) {
                List<DeathDetail> details = collect.getOrDefault(i, new ArrayList<>());
                yList.add(details.size());
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("xValue", xList);
        map.put("yValue", yList);
        return new ObjectResponse<>(map);
  • 2
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值