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

2023-03-23  本文已影响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]}}

上一篇 下一篇

猜你喜欢

热点阅读