Split the year and month to get the year and the month of the year

When developing recently, I encountered such a problem. Split the year, month, and day to obtain the year and the month with data in the current year. The
required data format is as follows

"data": [
    {
    
    
      "months": [
        "01"
      ],
      "year": "2023"
    },
    {
    
    
      "months": [
        "12",
        "11"
      ],
      "year": "2022"
    }
  ]

Because every time you go to the database to check the data, the year, month and day are connected together, such as "2022-01-01" and only the year and month are needed to be taken out.
If you take out "2022-01-01", "2022-02- 01", "2023-02-01", . Such a year, month, and day. How to deal with it
Finally, I thought of splitting the first four digits of the characters into the year as a key, and then use this key to match this array to get all the dates of this year. Then these dates are split out and the month is used as the value.
But there is another problem.
For example, if there are two dynamics in January, the date taken out is "2022-01-01" and "2022-01-02". Then you can't directly extract the month as the value and there will be repeated months. I think set can deduplication. Just put the value as a set collection. When it is obtained, it will be automatically deduplicated.
The final receiving set is made like this.

 Map<String, LinkedHashSet<String>> map = new HashMap<>();

then go to test

public class TestYear {
    
    
    public static void main(String[] args) {
    
    
        List<String> listTime = new ArrayList<>();
        listTime.add("2022-10-11");
        listTime.add("2022-11-11");
        listTime.add("2022-12-11");
        listTime.add("2023-10-11");
        listTime.add("2023-10-12");

        Map<String, LinkedHashSet<String>> map = new HashMap<>();

        for (String str : listTime) {
    
    
            String key = str.substring(0, 4);
            LinkedHashSet<String> list = map.get(key);
//如果map中对应key,创建String数组,并加到map中
            if (list == null || list.isEmpty()) {
    
    
                list = new LinkedHashSet<>();
                map.put(key, list);
            }
            list.add(str.substring(5,7));
        }
        System.out.println(map);
    }

}

Finally we get the result

{
    
    
2023=[10],
2022=[10, 11, 12]
 }

After getting this result, it is basically what we want, and then put in our own code to modify it a little bit. The following is the project code

public AjaxResult getYear() {
    
    
        Date nowDate = DateUtil.date().toSqlDate();
        List<String> listTime = new ArrayList<>();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd ");
        //获取当天日期前的所有活动
List<AppActivityInformation>listAc=this.list(Wrappers<AppActivityInformation>lambdaQuery().le(AppActivityInformation::getActivityTime,nowDate)
.orderByAsc(AppActivityInformation::getActivityTime));
        if (ObjectUtils.isNotEmpty(listAc)) {
    
    
        //循环把日期放到一个list集合中
            for (AppActivityInformation appActivityInformation : listAc) {
    
    
                Date activityTime = appActivityInformation.getActivityTime();
                String format = formatter.format(activityTime);
                listTime.add(format);
            }
            //取出来年月的集合
            Map<String, LinkedHashSet<String>> map = new HashMap<>();
            for (String str : listTime) {
    
    
                String key = str.substring(0, 4);
                LinkedHashSet<String> list = map.get(key);
//如果map中对应key,创建String数组,并加到map中
                if (list == null || list.isEmpty()) {
    
    
                    list = new LinkedHashSet<>();
                    map.put(key, list);
                }
                list.add(str.substring(5,7));
            }
            //把map对象放到我们自己的集合中
            List<YearMonth> yearMonths = new ArrayList<>();
            for (String key : map.keySet()) {
    
    
                YearMonth yearMonth = new YearMonth();
                yearMonth.setYear(key);
                LinkedHashSet<String> month = map.get(key);
                //HaseSet不能倒排我们就转下list再去倒排
                ArrayList<String> list = new ArrayList<String>(month);
                Collections.reverse(list);
               yearMonth.setMonths(list);
                yearMonths.add(yearMonth);
            }
            return AjaxResult.success(yearMonths);
        }
       return AjaxResult.success();
    }

Finally get the desired result.
Xiaobai, who has been developing for half a year, please give me your advice.

Guess you like

Origin blog.csdn.net/weixin_41438423/article/details/128575402