输入两个日期字符串,输出工作日的天数

    public Integer calLeaveDays(String startTimeStr, String endTimeStr) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date startTime = sdf.parse(startTimeStr);
        Date endTime = sdf.parse(endTimeStr);
        int leaveDays = 0;
        Calendar cal = Calendar.getInstance();
        while(startTime.compareTo(endTime)!=1){
            cal.setTime(startTime);
            //判断是否为周六日
            int week = cal.get(Calendar.DAY_OF_WEEK) - 1;
            if(week == 0 || week == 6){//0为周日,6为周六
                //跳出循环进入下一个日期
                cal.add(Calendar.DAY_OF_MONTH, +1);
                startTime = cal.getTime();
                continue;
            }
            //不是周末,天数+1
            leaveDays = leaveDays + 1;
            //日期往后加一天
            cal.add(Calendar.DAY_OF_MONTH, +1);
            startTime = cal.getTime();
        }
        return leaveDays;
    }

Q.E.D.

知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议

To see the world as it is and to love it.