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); }
标签:map,节假日,Calendar,代码,dateList,生成,simdf,calendar,isOffDay From: https://www.cnblogs.com/wmy666/p/17654166.html