计算当天到月末有多少天工作日

package com.hrocloud.tgpayroll.test;

import org.apache.commons.lang.StringUtils;
import org.junit.Test;
import com.hrocloud.tgpayroll.utils.Utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class test {

    private Utils utils;
    @Test
    public void display() throws ParseException {
        String weekend = "2020-02-18"; //输入当前日期
        int year = Integer.valueOf(weekend.substring(0,4));
        int month = Integer.valueOf(weekend.substring(5,7));
        int day = Integer.valueOf(weekend.substring(8,10));//今天
        Calendar c = Calendar.getInstance();
        c.set(year,month, 0);
        int dayOfMonth = c.get(Calendar.DAY_OF_MONTH);//这个月的总天数
        int xiuxi = isWeekend(weekend);//当天到月末的非工作日
        System.out.println("当天到月末除去工作日有"+(dayOfMonth-day-xiuxi+1)+"天");
    }

    //计算当天到月末有多少天非工作日
    public  int isWeekend( String weekend) throws ParseException {
        int year = Integer.valueOf(weekend.substring(0,4));
        int mouth = Integer.valueOf(weekend.substring(5,7));
        int day = Integer.valueOf(weekend.substring(8,10));
        Calendar c = Calendar.getInstance();
        c.set(year,mouth, 0);
        int dayOfMonth = c.get(Calendar.DAY_OF_MONTH);//当月有多少天
        int i=day;
        int b = 0;
        while (i<=dayOfMonth){
            StringBuffer sb = new StringBuffer();
            //待优化
            if(i==1||i==2||i==3||i==4||i==5||i==6||i==7||i==8||i==9){
                if(mouth==1||mouth==2||mouth==3||mouth==4||mouth==5||mouth==6||mouth==7||mouth==8||mouth==9){
                    sb.append(year).append("0"+mouth).append("0"+i);
                }else {
                    sb.append(year).append(mouth).append("0"+i);
                }
            }else {
                if(mouth==1||mouth==2||mouth==3||mouth==4||mouth==5||mouth==6||mouth==7||mouth==8||mouth==9){
                    sb.append(year).append("0"+mouth).append(i);
                }else {
                    sb.append(year).append(mouth).append(i);
                }
            }
            if(yanzheng(sb+"")){
                b++;
            }
            i++;
            sb = new StringBuffer();
        }
       return b;
    }

//判断是否是周末
    public boolean yanzheng(String weekend) throws ParseException {
        DateFormat format1 = new SimpleDateFormat("yyyyMMdd");
        Date bdate = format1.parse(weekend);
        Calendar cal = Calendar.getInstance();
        cal.setTime(bdate);
        if(cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY){
            return true;
        } else{
            return false;
        }
    }

}

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小轩爱编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值