您好,登錄后才能下訂單哦!
這篇文章主要介紹“java如何獲取某年內(nèi)工作日、休息日、節(jié)假日,返回json”,在日常操作中,相信很多人在java如何獲取某年內(nèi)工作日、休息日、節(jié)假日,返回json問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”java如何獲取某年內(nèi)工作日、休息日、節(jié)假日,返回json”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!
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節(jié)假日 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; } } /** * 獲取某年內(nèi)工作日、休息日、節(jié)假日 * * @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); //節(jié)假日 } } for (String wStr:makeWork) { if(wStr.equals(sdf.format(work))){ day1.setType(0); //補(bǔ)班 } } 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) { //節(jié)假日 List<String> holiday = new ArrayList<>(); //補(bǔ)班 List<String> makeWork = new ArrayList<>(); //元旦 holiday.add("2020-01-01"); //春節(jié) 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"); //端午節(jié) 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"); //生成工作日、周末、節(jié)假日 List<Day> dates = getDates(2020,holiday,makeWork); System.out.println(dates.size()); String s = JSON.toJSONString(dates); System.out.println(s); } }
到此,關(guān)于“java如何獲取某年內(nèi)工作日、休息日、節(jié)假日,返回json”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。