List<Map<String,Object>>分组,并获取每个月的最大值,获取上个月和指定月份的天数

该代码段使用Java进行月份数据处理,包括获取当前年份、遍历12个月并计算指定日期或上个月的数据。通过baseMapper查询数据,统计每个用户在各个月份的贡献次数,并找出每月最大贡献数。同时,计算指定年份和月份的总天数。
Calendar calendar = Calendar.getInstance();
        String dataTime =(String) params.get("dataTime");
        if (StringUtils.isBlank((String)params.get("dataTime"))){
            dataTime=String.valueOf(calendar.get(Calendar.YEAR));
        }
        List<Map<String,Object>> resutList = new ArrayList<>();
        String userId = StpUtil.getLoginIdAsString();


        for (int i = 1; i <=12 ; i++) {
            Map<String,Object> monthMap = new HashMap<>();

            String data = dataTime + "-" + (i>9?i:"0"+i);
            params.put("dataTime",data);

            List<Map<String, Object>> infoList = baseMapper.getInfoList(params);

            Integer myContribute=0;
            // 获取我的贡献总数
            for (Map<String,Object> map:infoList ){
                if (map.containsValue(userId)){
                    myContribute++;
                }
            }


            Map<String, List<Map>> map = infoList .stream().collect(Collectors.groupingBy(e -> e.get("addUserId").toString()));

            List<String> result = new ArrayList<String>();

            for(Map.Entry<String, List<Map>> entry : map.entrySet()) {
                List<Map> mapValue = entry.getValue();
                result.add(String.valueOf(mapValue.size()));
            }

            String max = "0";
            if (!CollectionUtils.isEmpty(result)){
                max = Collections.max(result);
            }
            monthMap.put("monthMax",max);
            monthMap.put("month",i+"月");
            // 我的贡献条数
            monthMap.put("myContribute",myContribute);
            resutList.add(monthMap);
        }

        return resutList;

  获取上个月

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        // 设置为当前时间
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, -1);
        // 设置为上一个月
        //calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);
        date = calendar.getTime();
        String dateTime = format.format(date);

 指定月份,有多少天

public class Main {
    public static int getDaysInMonth(int year, int month) {
        YearMonth yearMonthObject = YearMonth.of(year, month);
        return yearMonthObject.lengthOfMonth();
    }
  
    public static void main(String[] args) {
        int year = 2021;
        int month = 6;
        int days = getDaysInMonth(year, month);
        System.out.println("Days in month " + month + " of year " + year + " is " + days);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值