package cn.ffcs.demand.util;
import java.text.DecimalFormat;
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.List;
import java.util.ResourceBundle;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.time.DateUtils;
/**
* 本类是一个工作日计算类
* 本类负责:1计算某个时间点加上相应的工作日后就会生成一个非假期的工作日期
* 2计算两个日期的具体工作日
* 3计算工作的小时数(排除双休节假日,涵盖了周末上班情况)
* @author 刘强
* @version Ver 1.0 2018-06-29 改订
*
*/
public class WorkDayPlus {
/**
* 此方法计算某个日期加上几个工作日后的一个工作日期(除周末)
* @param date(起始日期) , day(要添加的工作天数)
* @return incomeDate(去除周末后的日期)
*/
public Date getIncomeDate(Date date,int days) throws NullPointerException{
Date incomeDate = date;
for(int i = 1 ; i <= days ; i++){
incomeDate = getIncomeDate(incomeDate);
}
return incomeDate;
}
/**
* 此方法计算某个日期加上几个工作日后的一个工作日期(除周末和法定节假日)
* @param date(起始日期) , day(要添加的工作天数)
* @return incomeDate(去除周末后的日期)
*/
public Date getIncomeDatePlus(Date date,int days) throws NullPointerException{
Date incomeDate = date;
for(int i = 1 ; i <= days ; i++){
incomeDate = getIncomeDatePlus(incomeDate);
}
return incomeDate;
}
/**
* 此方法计算某个日期后一天的工作日期(除周末)
* @param date(起始日期)
* @return incomeDate(去除周末后的日期)
*/
private Date getIncomeDate(Date date) throws NullPointerException{
if (null == date){
throw new NullPointerException("the date is null or empty!");
}
//对日期的操作,我们需要使用 Calendar 对象
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
//+1天
calendar.add(Calendar.DAY_OF_MONTH, +1);
//判断是星期几
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
Date incomeDate = calendar.getTime();
if (dayOfWeek == 1 || dayOfWeek == 7){
//递归
return getIncomeDate(incomeDate);
}
return incomeDate;
}
/**
* 此方法计算某个日期后一天的工作日期(除周末和法定节假日)
* @param date(起始日期)
* @return incomeDate(去除周末后的日期)
*/
public Date getIncomeDatePlus(Date date) throws NullPointerException{
if (null == date){
throw new NullPointerException("the date is null or empty!");
}
//对日期的操作,我们需要使用 Calendar 对象
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
//+1天
calendar.add(Calendar.DAY_OF_MONTH, +1);
Date incomeDate = calendar.getTime();
if (isWeekend(calendar) || isHoliday(calendar)){
//递归
return getIncomeDatePlus(incomeDate);
}
return incomeDate;
}
/**
* 此方法计算两个日期的真实工作日(除周末和法定节假日)
* @param startTime(起始日期),endTime(结束时间)
* @return workdays(去除周末和法定节假日后的日期)
*/
public int getWorkDays(Date startTime,Date endTime){
return getDatesBetweenTwoDate(startTime,endTime).size();
}
/**
* 计算工时(小时)
* @param startTime
* @param endTime
* @return
*/
public int getWorkHours(Date startTime,Date endTime){
double workHour = 0;
DecimalFormat df = new DecimalFormat("######0");
int workDays = getDatesBetweenTwoDate(startTime,endTime).size();
if(workDays==1){
double hour = this.calHourBetweenTwoDate(startTime,endTime);
if(hour<7){
workHour = Integer.parseInt(df.format(hour));
}else if(hour>=7){
workHour = 7;
}
}else if(workDays>=2){
ResourceBundle resourceBundle = ResourceBundle.getBundle("WorkDayPlusConfig");
String goOffWorkTime = "";
if(isSummerTime(endTime)){
goOffWorkTime=resourceBundle.getString("SummerGoOffWorkTime");
}else if(!isSummerTime(endTime)){
goOffWorkTime=resourceBundle.getString("WinterGoOffWorkTime");
}
double lastDay = this.calHourBetweenTwoDate(this.chang2TodayTime(resourceBundle.getString("WorkStartTime"),endTime),endTime);
double firstDay = this.calHourBetweenTwoDate(startTime,this.chang2TodayTime(goOffWorkTime,startTime));
if(firstDay>7){
firstDay=7;
}
if(lastDay>7){
lastDay=7;
}
workHour = Integer.parseInt(df.format(firstDay+lastDay+(workDays-2)*7));
}
if(workHour<=1){
workHour=1;
}
return (int)workHour;
}
/**
* 判断一个日期是否是夏季时间
* @param date
* @return
*/
public boolean isSummerTime(Date date){
ResourceBundle resourceBundle = ResourceBundle.getBundle("WorkDayPlusConfig");
Calendar SummerbeginTime = String2Calendar(resourceBundle.getString("SummerBeginTime"));
Calendar WinterBeginTime = String2Calendar(resourceBundle.getString("WinterBeginTime"));
Calendar caldate = Calendar.getInstance();
caldate.setTime(date);
if (caldate.after(SummerbeginTime) && caldate.before(WinterBeginTime)) {
return true;
} else {
return false;
}
}
/**
* String转Calendar
* @param sdate
* @return
*/
public Calendar String2Calendar(String sdate){
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date cdate = null;
try {
cdate = sdf.parse(sdate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(cdate);
return calendar;
}
/**
* 计算两个时间差(小时)
* @param startTime
* @param endTime
* @return
*/
public double calHourBetweenTwoDate(Date startTime,Date endTime){
long nh = 1000 * 60 * 60;
long diff = endTime.getTime() - startTime.getTime();
double hour = (double)diff/nh;
return hour;
}
/**
* 生成一个上下班时间,因为夏季冬季不一样
* @param time
* @return
*/
private Date chang2TodayTime(String timeStr,Date sedate){
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
String dateStr=sdf.format(sedate);
String date = dateStr + " " + timeStr;
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date datetime = null;
try {
datetime= df.parse(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return datetime;
}
/**
* 判断一个日期是不是周末.
* @param Calendar calendar(要判断的日期)
* @return Boolean(返回true为是周末)
*/
private boolean isWeekend(Calendar calendar){
//判断是星期几
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
if (dayOfWeek == 1 || dayOfWeek == 7){
if(isWeekendWorkDays(calendar)){
return false;
}else{
return true;
}
}
return false;
}
/**
* 一个日历是不是法定节假日
* WorkDayPlusConfig.properties为配置文件,其中存放了法定节假日期,格式为:holiday=2018-10-1,...,..
* @param Calendar calendar(要判断的日期)
* @return Boolean(返回true为是法定节假日)
*/
private boolean isHoliday(Calendar calendar){
String pattern = "yyyy-MM-dd";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String dateString = simpleDateFormat.format(calendar.getTime());
//节假日 这个可能
评论5
最新资源