Java计算一段时间内扣除节假日和周末的时间间隔

Java计算一段时间内扣除节假日和周末的时间间隔

public String calculateTime() {
        HttpServletRequest request = ServletActionContext.getRequest();
        String actinstId = "";
        if (StringUtils.isNotEmpty(request.getParameter("actinstId"))) {
            actinstId = request.getParameter("actinstId");
        }
        String nextStepName = "";
        if (StringUtils.isNotEmpty(request.getParameter("nextStepName"))) {
            nextStepName = request.getParameter("nextStepName");
        }
        //首先获取该活动的开始时间和结束时间,并求时间差值
        List mngAct = iExtendCaseQuery.queryDeptActByActinstId(actinstId);
        Object[] object = mngAct.get(0);
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date actStime = null;
        Date actEtime = null;
        String deptId = null;
        List dateList = new ArrayList();
        DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        long diff = 0L;
        long seconds = 0L;
        try {
            deptId = object[2].toString();
            actStime = df.parse(object[0].toString());
            actEtime = df.parse(object[1].toString());
            diff = actEtime.getTime() - actStime.getTime();//这样得到的差值是微秒级别
            seconds = diff / 1000;
            logger.warn("不扣除节假日的时间差:" + seconds + "秒");
            //将时间段的每一天放入数组中
            calendar.setTime(df1.parse(object[1].toString()));
            dateList.add((Date) object[1]);
            while (calendar.getTime().after(df1.parse(object[0].toString()))) {
                calendar.add(Calendar.DAY_OF_MONTH, -1);
                dateList.add(calendar.getTime());
            }
            for (int i = 0; i < dateList.size(); i++) {
                Date temptime = dateList.get(i);
                calendar.setTime(temptime);
                if (calendar.get(Calendar.DAY_OF_WEEK) == 1 || calendar.get(Calendar.DAY_OF_WEEK) == 7) {//1:周日 7:周六
                    seconds = seconds - 24 * 60 * 60;
                }
            }
            logger.warn("扣除周末的时间差:" + seconds + "秒");
        } catch (Exception e) {
            e.printStackTrace();
        }

        //查询节假日表,判断开始时间和结束时间之间是否包含节假日,若包含,看有几个
        List holidayTime = iExtendCaseQuery.queryHolidayTime();
        Object[] objectHoliday = null;
        Date holidayStime = null;
        Date holidayEtime = null;
        for (int i = 0; i < holidayTime.size(); i++) {
            objectHoliday = holidayTime.get(i);
            try {
                holidayStime = df.parse(objectHoliday[0].toString());
                holidayEtime = df.parse(objectHoliday[1].toString());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            if (holidayStime.getTime() >= actStime.getTime() && holidayEtime.getTime() <= actEtime.getTime()) {
                seconds = seconds - (holidayEtime.getTime() - holidayStime.getTime()) / 1000;
            }
        }
        logger.warn("扣除节假日的时间差(包含周末和非工作时间):" + seconds + "秒");
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setCharacterEncoding("UTF-8");
        PrintWriter pw = null;
        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/xml");
            pw = response.getWriter();
            pw.write("ok");
            pw.flush();
            pw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

你可能感兴趣的:(Java)