某一年节假日生成代码

public static void main(String[] args) throws Exception {
        int year = 2023;
        List<String> dateList=new ArrayList<String>();
        SimpleDateFormat simdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = new GregorianCalendar(year, 0, 1);
        String a = HttpClient.doGet("https://api.jiejiariapi.com/v1/holidays/2023");
        JSONObject jsonObject = JSONObject.fromObject(a);
        int i = 1;
        while (calendar.get(Calendar.YEAR) < year + 1) {
            calendar.set(Calendar.WEEK_OF_YEAR, i++);
            calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
            if (calendar.get(Calendar.YEAR) == year) {
                Map<String, Object> map = (Map<String, Object>)jsonObject.get(simdf.format(calendar.getTime()));
                if(map != null && map.size() != 0){
                    boolean isOffDay = (boolean)map.get("isOffDay");
                    if(!isOffDay){
                        System.out.println("周日:"+simdf.format(calendar.getTime()));
                    }else{
                        dateList.add(simdf.format(calendar.getTime()));
                    }
                }else{
                    dateList.add(simdf.format(calendar.getTime()));
                }
            }
            calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
            if (calendar.get(Calendar.YEAR) == year) {
                Map<String, Object> map = (Map<String, Object>)jsonObject.get(simdf.format(calendar.getTime()));
                if(map != null && map.size() != 0){
                    boolean isOffDay = (boolean)map.get("isOffDay");
                    if(!isOffDay){
                        System.out.println("周六:"+simdf.format(calendar.getTime()));
                    }else{
                        dateList.add(simdf.format(calendar.getTime()));
                    }
                }else{
                    dateList.add(simdf.format(calendar.getTime()));
                }
            }
        }

        System.out.println(dateList.size());
        System.out.println(dateList);

        Map<String, Map<String, Object>> map = (Map<String, Map<String, Object>>)jsonObject;
        map.forEach((k,v)->{
            boolean isOffDay = (boolean)v.get("isOffDay");
            if(isOffDay){
                dateList.add(k);
            }
        });
        //集合去重排序
        List<String> dateList1 = dateList.stream().distinct().sorted().collect(Collectors.toList());
        System.out.println(dateList1.size());
        System.out.println(dateList1);


    }

 

posted @ 2023-08-24 15:01  噗噗噗i丶  阅读(39)  评论(0编辑  收藏  举报