java后台读取json文件 判断是否工作日,当前月份有多少天工作日

编写json文件

编写 holiday.json 需要一年更新一次,将所有的法定节假日输入到dates中,将所有周末调休日期输入到workday中,具体格式参照如下:

[
    {
        "year": "2020",
        "dates": ["2020-01-01","2020-01-24","2020-01-25","2020-01-26","2020-01-27","2020-01-28","2020-01-29","2020-01-30",
         "2020-01-31","2020-02-01","2020-02-02",
         "2020-04-04","2020-04-05","2020-04-06",
         "2020-05-01","2020-05-02","2020-05-03","2020-05-04","2020-05-05",
         "2020-06-25","2020-06-26","2020-06-27",
         "2020-10-01","2020-10-02","2020-10-03","2020-10-04","2020-10-05","2020-10-06","2020-10-07","2020-10-08"],
        "workday":["2020-01-19","2020-04-26","2020-05-09","2020-06-28","2020-09-27","2020-10-10"] 
    }
   
]

读取json文件判断是否为节假日,是否周末,是否调休上班

	/** 
	 * 读取json文件方法
	 * @return 
	 */  
	private static String jsonRead(File file){
		Scanner scanner = null;
		StringBuilder buffer = new StringBuilder();
		try {
			scanner = new Scanner(file, "utf-8");
			while (scanner.hasNextLine()) {
				buffer.append(scanner.nextLine());
			}
		} catch (Exception e) {

		} finally {
			if (scanner != null) {
				scanner.close();
			}
		}
		return buffer.toString();
	}

	/** 
	 * 判断是否是节假日
	 * @return 
	 */  
	public static boolean isHoliday(Date date){ 
		//加载json文件
		String jsonData = "";
		try {
			ClassPathResource resource = new ClassPathResource("holiday.json");
			File file = resource.getFile();
			jsonData = jsonRead(file);
			
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int year = cal.get(Calendar.YEAR);
		JSONArray holiday = JSONArray.fromObject(jsonData);
		for(int i=0;i<holiday.size();i++) {
			JSONObject myjObject = holiday.getJSONObject(i);
			if(myjObject.optString("year").equals(String.valueOf(year))) {
				String date1 = dateToStr(date);
				if(myjObject.optString("dates").toString().contains(date1)) {
					 return true;
				}
			}
		}
		return false; 
	} 
	
	/** 
	 * 判断是否是周末 
	 * @return 
	 */  
	public static boolean isWeekend(Date date){  
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		
	    int week=cal.get(Calendar.DAY_OF_WEEK)-1;  
	    if(week ==6 || week==0){//0代表周日,6代表周六  
	        return true;  
	    }  
	    return false;  
	}  

	/** 
	 * 判断是否调休上班
	 * @return 
	 */  
	public static boolean isWork(Date date){ 
		//加载json文件
		String jsonData = "";
		try {
			ClassPathResource resource = new ClassPathResource("holiday.json");
			File file = resource.getFile();
			jsonData = jsonRead(file);
			
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int year = cal.get(Calendar.YEAR);
		JSONArray holiday = JSONArray.fromObject(jsonData);
		for(int i=0;i<holiday.size();i++) {
			JSONObject myjObject = holiday.getJSONObject(i);
			if(myjObject.optString("year").equals(String.valueOf(year))) {
				String date1 = dateToStr(date);
				if(myjObject.optString("workday").toString().contains(date1)) {
					return true;
				}
			}
		}
		return false; 
	} 

判断是否为工作日

	/** 
	 * 判断是否工作日
	 * @param 日期Date
	 * @return true/false
	 */  
	public static boolean isWorkDay(Date date){  
		if((!isWeekend(date)&&!isHoliday(date))||isWork(date))
			return true;
		return false;
	}  

获取指定年月有多少个工作日

    /**
     * 获取指定年月有多少个工作日
     *
     * @param year
     * @param month
     * @return count
     */
    public static int countWorkDay(int year, int month) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        // 月份是从0开始计算,所以需要减1
        c.set(Calendar.MONTH, month - 1);
 
        // 当月最后一天的日期
        int max = c.getActualMaximum(Calendar.DAY_OF_MONTH);
        // 开始日期为1号
        int start = 1;
        // 计数
        int count = 0;
        while (start <= max) {
            c.set(Calendar.DAY_OF_MONTH, start);
            if ((!isWeekend(c.getTime())&&!isHoliday(c.getTime()))||isWork(c.getTime())) {
                count++;
            }
            start++;
        }
        return count;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值