欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

请假功能中计算有效工作日

程序员文章站 2022-05-18 07:55:01
...

判断请假日期中有效工作日:排除周末及法定节日
1) 引用方法

String timeDates[] = request.getParameter("timeDate").split(",");
        String timeDate=timeDates[0]+"-"+timeDates[1]+"-"+timeDates[2];
        String timeDate2=timeDates[3]+"-"+timeDates[4]+"-"+timeDates[5];
        int  times= dayutils.weekDayCount(timeDate, timeDate2);//weekDayCoun方法支持date类型和String类型的日期传入

2)weekDayCount方法实现
首先通过配置文件配置2017年周末情况
附:weekDayConfig.xml

<?xml version="1.0" encoding="utf-8"?>
<root>
    <validation>
        <list>
            <!-- 非补报报文的交易时间必须在5个工作日内 -->
            <key>EXCH_DATE_CHECK</key>
            <value>5</value>
        </list>
        <list>
            <!-- 回执处理必须在5个工作日内 -->
            <key>REC_DATE_CHECK</key>
            <value>5</value>
        </list>
    </validation>
    <!-- 工作日是假期的情况 -->
    <weekday>
        <holiday_list>
            <date>2017-01-02</date>
            <date>2017-01-27</date>
            <date>2017-01-30</date>
            <date>2017-01-31</date>
            <date>2017-02-01</date>
            <date>2017-02-02</date>
            <date>2017-04-03</date>
            <date>2017-04-04</date>
            <date>2017-05-01</date>
            <date>2017-05-29</date>
            <date>2017-05-30</date>
            <date>2017-10-02</date>
            <date>2017-10-03</date>
            <date>2017-10-04</date>
            <date>2017-10-05</date>
            <date>2017-10-06</date>
        </holiday_list>
    </weekday>
    <!-- 周末是工作日的情况 -->
    <weekend>
        <weekday_list>
            <date>2017-01-22</date>
            <date>2017-02-04</date>
            <date>2017-04-01</date>
            <date>2017-05-27</date>
            <date>2017-09-30</date>
        </weekday_list>
    </weekend>
</root>

附:WeekdayVO.java

package com.buss.common;

import java.util.List;
import java.util.Map;

public class WeekdayVO {

    private Map validateMap;//工作日校验
    private List weekendIsWeekdayList;//周末是工作日的列表
    private List weekdayIsHolidayList;//工作日是假期的列表


    public Map getValidateMap() {
        return validateMap;
    }
    public void setValidateMap(Map validateMap) {
        this.validateMap = validateMap;
    }
    public List getWeekendIsWeekdayList() {
        return weekendIsWeekdayList;
    }
    public void setWeekendIsWeekdayList(List weekendIsWeekdayList) {
        this.weekendIsWeekdayList = weekendIsWeekdayList;
    }
    public List getWeekdayIsHolidayList() {
        return weekdayIsHolidayList;
    }
    public void setWeekdayIsHolidayList(List weekdayIsHolidayList) {
        this.weekdayIsHolidayList = weekdayIsHolidayList;
    }


}

附:WeekDayUtil.java

package com.buss.common;

import java.io.File;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class WeekDayUtil {

    private final static String BASE_PATH = "/classes/resources/";

    /**
     * @title 获取工作日相关配置
     * @author chanson
     * @return
     */
    public WeekdayVO getWeekdayConfig(){

        Map validateMap = new HashMap();
        List weekendIsWeekdayList = new ArrayList();
        List weekdayIsHolidayList = new ArrayList();

        //web相对路径+xml文件路径
        //1、放到web工程
        File f = new File("WEB-INF/classes/weekDayConfig.xml");
        //File f = new File("D:/apache-tomcat-7.0.33/apache-tomcat-7.0.33/webapps/lzhrms/WEB-INF/classes/weekDayConfig.xml");
        String path=f.getAbsolutePath().toString();
        String  path1[]=path.split("bin");
        path1[0]=path1[0].replaceAll("\\\\","/");
        String  p=path1[0]+"webapps/lzhrms/WEB-INF/classes/weekDayConfig.xml";
        f = new File(p);
        //2、application测试
        //String file  = "D:/workspace/test/src/config/weekday.xml";
        //File f = new File(file);
        SAXReader reader = new SAXReader();
        try{
            Document doc = reader.read(f);
            Element root = doc.getRootElement();

            //===================================
            //工作日校验相关属性
            //===================================
            Element validationElement = root.element("validation");
            Element listElement;
            for(Iterator i = validationElement.elementIterator("list");i.hasNext();){
                listElement = (Element)i.next();
                validateMap.put((String) listElement.elementText("key"), 
                        (String)listElement.elementText("value"));
            }

            //===================================
            //工作日是假期的列表
            //===================================
            Element weekdayElement = root.element("weekday");
            Element holidayListElement = weekdayElement.element("holiday_list");
            Element holidayValueElement = null;
            for(Iterator i = holidayListElement.elementIterator("date");i.hasNext();){
                holidayValueElement = (Element)i.next();
                weekdayIsHolidayList.add((String)holidayValueElement.getText());
            }

            //===================================
            //周末是工作日的列表
            //===================================
            Element weekendElement = root.element("weekend");
            Element weekdayListElement = weekendElement.element("weekday_list");
            Element weekdayValueElement = null;
            for(Iterator i = weekdayListElement.elementIterator("date");i.hasNext();){
                weekdayValueElement = (Element)i.next();
                weekendIsWeekdayList.add((String)weekdayValueElement.getText());
            }

            WeekdayVO vo = new WeekdayVO();
            vo.setValidateMap(validateMap);
            vo.setWeekdayIsHolidayList(weekdayIsHolidayList);
            vo.setWeekendIsWeekdayList(weekendIsWeekdayList);
            return vo;
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }
    }

     /** 
     * @title 判断是否为工作日 
     * @detail 工作日计算: 
     *           1、正常工作日,并且为非假期 
     *           2、周末被调整成工作日 
     * @author yy 
     * @param date 日期 
     * @return 是工作日返回true,非工作日返回false 
     */
    public static boolean isWeekday(Date time){  
        WeekDayUtil util = new WeekDayUtil();  
        WeekdayVO vo = util.getWeekdayConfig();  
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(time);

        if (c.get(GregorianCalendar.DAY_OF_WEEK) != GregorianCalendar.SATURDAY  
                && c.get(GregorianCalendar.DAY_OF_WEEK) != GregorianCalendar.SUNDAY){  
            //平时  
            return !vo.getWeekdayIsHolidayList().contains(sdf.format(time));  
        }else{  
            //周末  
            return vo.getWeekendIsWeekdayList().contains(sdf.format(time));  
        }  
    }

    /** 
     * @title 判断是否为工作日 
     * @detail 工作日计算: 
     *           1、正常工作日,并且为非假期 
     *           2、周末被调整成工作日 
     * @author yy 
     * @param String 日期 
     * @return 是工作日返回true,非工作日返回false 
     */
    public static boolean isWeekday(String timeStr){ 
        WeekDayUtil util = new WeekDayUtil();  
        WeekdayVO vo = util.getWeekdayConfig();  
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        boolean weekday=false;
        try {
            c.setTime(sdf.parse(timeStr));
            if (c.get(GregorianCalendar.DAY_OF_WEEK) != GregorianCalendar.SATURDAY  
                    && c.get(GregorianCalendar.DAY_OF_WEEK) != GregorianCalendar.SUNDAY){  
                //平时  
                weekday= !vo.getWeekdayIsHolidayList().contains(timeStr);
            }else{  
                //周末  
                weekday= vo.getWeekendIsWeekdayList().contains(timeStr);  
            }
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return weekday;
    }

    /** 
     * @title 获取当前月工作日天数
     * @detail 工作日计算: 
     *           1、获取月份总天数 
     *           2、循环每天,判断是否为工作日
     * @author yy
     * @param String 日期月份(2017-01) 
     * @return int 天数
     */
    public static int monthWeekDaySum(String timeStr){
        int count = 0;
        int month = Integer.parseInt(timeStr.substring(5, 7));
        Calendar c = Calendar.getInstance();

        c.set(Calendar.YEAR, Integer.parseInt(timeStr.substring(0, 4)));
        c.set(Calendar.MONTH,  month - 1);
        c.set(Calendar.DATE, 1);

        while(c.get(Calendar.MONTH) < month){
            if(isWeekday(c.getTime())){
                count++;
            }
            c.add(Calendar.DATE, 1);
        }

        return count;
    }

    /** 
     * @title 获取当前月工作日天数
     * @detail 工作日计算: 
     *           1、获取月份总天数 
     *           2、循环每天,判断是否为工作日
     *           3、判断时使用工作日和XML两种方式结合
     * @author yy
     * @param Date 日期月份
     * @return int 天数
     */
    public static int monthWeekDaySum(Date time){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        return monthWeekDaySum(sdf.format(time));
    }

    /** 
     * @title 计算两个时间内工作日天数
     * @detail 工作日计算 (包含开始时间以及结束时间)
     * @author yy
     * @param Date 开始时间   Date 结束时间
     * @return int 天数
     */
    public static int weekDayCount(Date startTime,Date endTime){
         Calendar sc = Calendar.getInstance();
         sc.setTime(startTime);

         Calendar ec = Calendar.getInstance();
         ec.setTime(endTime);

         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
         int count=0;
         while(sc.getTime().before(ec.getTime())){
            if(isWeekday(sc.getTime())){
                count++;
            }
            sc.add(Calendar.DATE, 1);
         }
         if(isWeekday(sc.getTime())){
             count++;
         }

        return count;
    }

    /** 
     * @title 计算两个时间内工作日天数
     * @detail 工作日计算 (包含开始时间以及结束时间)
     * @author yy
     * @param String 开始时间   String 结束时间  格式:yyyy-MM-dd
     * @return int 天数
     */
    public static int weekDayCount(String startTime,String endTime){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date start;
        Date end;
        int count=0;
        try {
            start=sdf.parse(startTime);
            end = sdf.parse(endTime);
            count=weekDayCount(start,end);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 判断当前日期是星期几
     * 
     * @param pTime 修要判断的时间
     * @return dayForWeek 判断结果
     * @Exception 发生异常
     */
     public static int dayForWeek(String pTime) throws Exception {
          SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
          Calendar c = Calendar.getInstance();
          c.setTime(format.parse(pTime));
          int dayForWeek = 0;
          if(c.get(Calendar.DAY_OF_WEEK) == 1){
           dayForWeek = 7;
          }else{
           dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
          }
          return dayForWeek;
     }

    public static void main(String[] args) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println(weekDayCount(sdf.parse("2017-10-01"),sdf.parse("2017-10-10")));
//      System.out.println(weekDayCount("2017-10-01","2017-10-10"));

//      System.out.println(dayForWeek("2017-06-03"));
//      System.out.println(isWeekday("2017-10-04"));
//      System.out.println(monthWeekDaySum(new Date()));
    }

}
相关标签: string 工作