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;
}
}
}
计算当天到月末有多少天工作日
最新推荐文章于 2022-10-08 17:22:28 发布