用java写一个算工作日期的功能(考虑到节假日以及补班的情况)
最近公司需要用java写一个算工作日的功能,简单说说我的实现过程跟思路,以供各位参考,大牛勿喷。
基本思路:在数据库先创建一个表,在表中初始化一年中的假期、需要补班等数据(因为每一年放假、补办安排都不一样,所以每年都要手动初始化数据进去),然后后台接收到开始时间跟结束时间这两个参数,计算出起始时间中的天数,减去法定节假日跟周末(有一点要注意一下,周末跟法定节假日是同一天),再加上补班的天数,这样可得出在一个时间段中,此人究竟上了多少天班。废话不多说了,直接上代码:
1.数据库建表后创建实体类并添加get,set方法:
2.dao层跟service层添加增删改查方法,并在实现类中实现各方法,
可根据业务不同,自行设计相应的方法。
3.直接上模拟debug的方法(本地可直接测试):
public class WorkDayUtilsDebug {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date startDate = sdf.parse("2017-03-19");
Date endDate = sdf.parse("2017-04-19");
List<Date> betweenDates = getBetweenDates(startDate, endDate);
for (int i = 0; i < betweenDates.size(); i++) {
Date date = betweenDates.get(i);
int holidayTypeByDate = getHolidayTypeByDate(date);
// type为-1:则说明在数据库没有匹配到设置,则由程序决定是否为假日
if (holidayTypeByDate == -1) {
// 如果是否为周末,则为节假日
if (isWeekend(date)) {
holidayTypeByDate = 0;
} else {
holidayTypeByDate = 1;
}
}
switch (holidayTypeByDate) {
// 0:即为节假日,则减掉日期
case 0:
System.out.println(sdf.format(date) + "节假日");
betweenDates.remove(i);
i--;
break;
// 1:补班,则加上日期(不用动作处理)
case 1:
break;
}
}
System.out.println(
sdf.format(startDate) + " 至 " + sdf.format(endDate) + ",工作天数为:" + betweenDates.size());
}
/**
* 判断是否是周末
*
* @return
*/
private static boolean isWeekend(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int week = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (week == 6 || week == 0) {// 0代表周日,6代表周六
return true;
}
return false;
}
/**
* @Name getHolidayTypeByDate
* @Description 传递日期,获取人工设置表中是为有业务动作
* @Author liusheng
* @Version V1.00
* @CreateDate 2017年3月12日 下午9:52:26
* @param date
* @return 0:即为节假日 1:补班
*/
public static int getHolidayTypeByDate(Date date) {
/* ----------------------test 代码 start ---------------------- */
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
long time = 0;
try {
time = sdf.parse(sdf.format(date)).getTime();
} catch (ParseException e1) {
e1.printStackTrace();
}
// 假设"2017-03-13 2017-03-12" 就是补班
Date startDate = null;
try {
startDate = sdf.parse("2017-03-10");
} catch (ParseException e) {
e.printStackTrace();
}
Date endDate = null;
try {
endDate = sdf.parse("2017-03-12");
} catch (ParseException e) {
e.printStackTrace();
}
if (time >= startDate.getTime() && time <= endDate.getTime()) {
return 1;
}
// 假设"2017-10-01 2017-10-07" 就是补班
try {
startDate = sdf.parse("2017-10-01");
} catch (ParseException e) {
e.printStackTrace();
}
try {
endDate = sdf.parse("2017-10-07");
} catch (ParseException e) {
e.printStackTrace();
}
if (time >= startDate.getTime() && time <= endDate.getTime()) {
return 0;
}
/* ----------------------test 代码 end ---------------------- */
// 查不班就是-1,系统以周末为准
return -1;
}
/**
* 获取两个日期之间的日期
*
* @param start 开始日期
* @param end 结束日期
* @return 日期集合
*/
private static List<Date> getBetweenDates(Date start, Date end) {
List<Date> result = new ArrayList<Date>();
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(start);
tempStart.add(Calendar.DAY_OF_YEAR, 1);
Calendar tempEnd = Calendar.getInstance();
result.add(start);
tempEnd.setTime(end);
while (tempStart.before(tempEnd)) {
result.add(tempStart.getTime());
tempStart.add(Calendar.DAY_OF_YEAR, 1);
}
result.add(end);
return result;
}
}