美文网首页
时间列表转年月日的树形结构

时间列表转年月日的树形结构

作者: 私人云笔记_骁勇波波 | 来源:发表于2023-03-23 09:41 被阅读0次

private static void changeDateListToTreeMap(List<Date> dateList, Map<String, Object> map) {

        for (Date date : dateList) {

            LocalDate temp = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

            int year = temp.getYear();

            int month = temp.getMonthValue();

            int day = temp.getDayOfMonth();

            Map<String, Object> monthMap = (Map<String, Object>) map.get(year + "");

            if (CollectionUtils.isEmpty(monthMap)) {

                monthMap = new HashMap<String, Object>();

            }

            List<Integer> dayList = (List<Integer>)monthMap.get(month + "");

            if (CollectionUtils.isEmpty(dayList)) {

                dayList = new ArrayList<Integer>();

            }

            dayList.add(day);

            monthMap.put(month + "", dayList);

            map.put(year + "", monthMap);

        }

    }

转换后格式:

"data":{"2017":{"3":[16,17,18,23], "5":[16,17,18,23]},"2018":{"12":[6,20,21,22,29]}}

相关文章

网友评论

      本文标题:时间列表转年月日的树形结构

      本文链接:https://www.haomeiwen.com/subject/xdhirdtx.html