您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要介绍“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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。