您好,登錄后才能下訂單哦!
這篇文章主要介紹“java如何獲取某年內工作日、休息日、節假日,返回json”,在日常操作中,相信很多人在java如何獲取某年內工作日、休息日、節假日,返回json問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”java如何獲取某年內工作日、休息日、節假日,返回json”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
package com.finesdk.util; import com.alibaba.fastjson.JSON; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; public class WorkDayUtil { public static class Day{ private String dayStr; //類型 0代表上班 1周末休息 2節假日 private Integer type; public String getDayStr() { return dayStr; } public void setDayStr(String dayStr) { this.dayStr = dayStr; } public Integer getType() { return type; } public void setType(Integer type) { this.type = type; } } /** * 獲取某年內工作日、休息日、節假日 * * @param year * @return */ private static List<Day> getDates(int year, List<String> holiday, List<String> makeWork) { List<Day> dates = new ArrayList<Day>(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); for (int i = 1; i <= 12; i++){ Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, year); cal.set(Calendar.MONTH, i - 1); cal.set(Calendar.DATE, 1); while (cal.get(Calendar.YEAR) == year && cal.get(Calendar.MONTH) < i) { int day = cal.get(Calendar.DAY_OF_WEEK); if ((day == Calendar.SUNDAY || day == Calendar.SATURDAY)) { Date work = (Date) cal.getTime().clone(); Day day1 = new Day(); day1.setDayStr(sdf.format(work)); day1.setType(1); //休息日 for (String hstr:holiday) { if(hstr.equals(sdf.format(work))){ day1.setType(2); //節假日 } } for (String wStr:makeWork) { if(wStr.equals(sdf.format(work))){ day1.setType(0); //補班 } } dates.add(day1); }else{ Date work = (Date) cal.getTime().clone(); Day day1 = new Day(); day1.setDayStr(sdf.format(work)); day1.setType(0); for (String hstr:holiday) { if(hstr.equals(sdf.format(work))){ day1.setType(2); } } dates.add(day1); } cal.add(Calendar.DATE, 1); } } return dates; } public static void main(String[] args) { //節假日 List<String> holiday = new ArrayList<>(); //補班 List<String> makeWork = new ArrayList<>(); //元旦 holiday.add("2020-01-01"); //春節 makeWork.add("2020-01-19"); holiday.add("2020-01-24"); holiday.add("2020-01-25"); holiday.add("2020-01-26"); holiday.add("2020-01-27"); holiday.add("2020-01-28"); holiday.add("2020-01-29"); holiday.add("2020-01-30"); holiday.add("2020-01-31"); holiday.add("2020-02-01"); holiday.add("2020-02-02"); //清明 holiday.add("2020-04-04"); holiday.add("2020-04-05"); holiday.add("2020-04-06"); //五一 makeWork.add("2020-04-26"); holiday.add("2020-05-01"); holiday.add("2020-05-02"); holiday.add("2020-05-03"); holiday.add("2020-05-04"); holiday.add("2020-05-05"); makeWork.add("2020-05-09"); //端午節 holiday.add("2020-06-25"); holiday.add("2020-06-26"); holiday.add("2020-06-27"); makeWork.add("2020-06-28"); //十一 makeWork.add("2020-09-27"); holiday.add("2020-10-01"); holiday.add("2020-10-02"); holiday.add("2020-10-03"); holiday.add("2020-10-04"); holiday.add("2020-10-05"); holiday.add("2020-10-06"); holiday.add("2020-10-07"); holiday.add("2020-10-08"); makeWork.add("2020-10-10"); //生成工作日、周末、節假日 List<Day> dates = getDates(2020,holiday,makeWork); System.out.println(dates.size()); String s = JSON.toJSONString(dates); System.out.println(s); } }
到此,關于“java如何獲取某年內工作日、休息日、節假日,返回json”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。