//计算小时数
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Double hours= HolidayDateUtil.calculateTimeHour(format.format(leave.getStart_time()),format.format(leave.getEnd_time()),"08:00:00","11:30:00","13:00:00","17:30:00");
Double day=0.0;
//计算半天
if(leave.getStart_time().getHours()<12){if(hours<=3.5){day=0.5;}else if(hours>3.5&&hours<=8){day=1.0;}else{day=Math.ceil(hours/8);}
}else{if(hours<=4.5){day=0.5;}else if(hours>4.5&&hours<=8){day=1.0;}else{day=Math.ceil(hours/8);}
}
leave.setDays(day);
leave.setCreate_user_id(userId);
leave.setCreate_user_name(userName);
int count = leaveMapper.add(leave);
if(count < 0 ){return RTN.rtn501("添加数据失败!");
}

------------------------------------------------------------------------------------------------------------------------------

HolidayDateUtil 放在util里
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;/*** 请假计算(小时)* 1.除去 节假日* 2.除去 其他规定的 非工作时间(有具体的  实行者指定)* 3.考虑一点 其他不上班时间 有些公司的特别* @author fengjing**/
public class HolidayDateUtil {/*** 节假日查询接口* 使用方法 url+yyyyMMdd*///时间格式化public final static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");public final static SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");//    /**
//     * 从公认网站上获取 节假日信息
//     * @param date 获取节假日的 公认网站 url="网站地址"+时间 时间格式yyyyMMdd
//     * @param
//     * @return 工作日对应结果为 0, 休息日对应结果为 1, 节假日对应的结果为 2, 网站失效-1, 数据格式改变-2
//     */
//    public static String getHoliday(String date) {
//        ChineseCalendarUtils cc = new ChineseCalendarUtils();
//        try {
//            return cc.checkDate(date);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return null;
//    }/*** 判断某年某月有多少天* @param year 年  "yyyy"* @param moun 月 "m"* @return*/public static int daysMonth(int year,int moun){switch(moun){case 0: return 31;case 1: return ((year%4==0&&year%100!=0)||year%400==0)?29:28;case 2: return 31;case 3: return 30;case 4: return 31;case 5: return 30;case 6: return 31;case 7: return 31;case 8: return 30;case 9: return 31;case 10: return 30;case 11: return 31;default: return 0;}}/*** 通过时间秒毫秒数判断两个时间的间隔* @param tiny 开始时间* @param large 结束时间* @return 返回几天*/public static int differentDaysByMillisecond(Date tiny,Date large){if(format1.format(tiny).equals(format1.format(large))){return 0;}Calendar c1 = Calendar.getInstance();Calendar c2 = Calendar.getInstance();c1.setTime(tiny);c2.setTime(large);int result = c2.get(Calendar.DAY_OF_YEAR) - c1.get(Calendar.DAY_OF_YEAR);/*Double why = Double.valueOf((large.getTime() - tiny.getTime()))/(1000*3600*24);double d = Math.ceil(why);*/return result;}/*** 通过时间秒毫秒数判断两个时间的间隔* @param tiny 开始时间* @param large 结束时间* @return 返回几小时*/public static double differentHoursByMillisecond(Date tiny,Date large){double hours = ((double) ((large.getTime() - tiny.getTime())) / (1000*3600));return hours;}/*** 时间格式yyyy-MM-dd HH:mm:ss* @param start 请假开始时间 yyyy-MM-dd HH:mm:ss* @param end 请假结束时间 yyyy-MM-dd HH:mm:ss* @param startM 上班早晨开始时间 HH:mm:ss* @param endE   上班早晨结束时间 HH:mm:ss* @param startAf 上班下午开始时间 HH:mm:ss* @param endAf      上班下午结束时间 HH:mm:ss* @return*/public static double calculateTimeHour(String start, String end, String startM, String endE, String startAf, String endAf){if(start!=null&&end!=null&&startM!=null&&endE!=null&&startAf!=null&&endAf!=null){//确保时间格式正确和值存在try {Date dataStart =cn.waifutong.supplyChain.util.HolidayDateUtil.format.parse(start);Date dataEnd = cn.waifutong.supplyChain.util.HolidayDateUtil.format.parse(end);double hours = 0;int monthDays = -1;int yearTime = dataStart.getYear()+1900;int mounthTime = dataStart.getMonth();int dataTime = dataStart.getDate();double lunchBreak;int interval = cn.waifutong.supplyChain.util.HolidayDateUtil.differentDaysByMillisecond(dataStart,dataEnd);//判断这年这月有多少天monthDays = HolidayDateUtil.daysMonth(yearTime,mounthTime);//第一天请假开始时间String startday = start.substring(start.lastIndexOf(" ")+1,start.length()-1);//最后一天请假结束时间String endday = end.substring(end.lastIndexOf(" ")+1,end.length()-1);double totalDay = 0;//累积总共请假多少天for(int i=0;i<=interval;i++){if(interval==0){//间隔0天 也就是 请假在一天之内
//                        if(!"0".equals(HolidayDateUtil.getHoliday(yearTime+"-"+(((mounthTime+1))>=10?(mounthTime+1):"0"+(mounthTime+1))+"-"+(dataTime>10?dataTime:"0"+dataTime)))){
//                            System.out.println(""+yearTime+(((mounthTime+1))>10?(mounthTime+1):"0"+(mounthTime+1))+(dataTime>10?dataTime:"0"+dataTime)+"非工作日期不需要计算");
//                            return 0;
//                        }String s = start.substring(0,10)+" "+endE;String s1 = start.substring(0,10)+" "+startAf;String s2 = start.substring(0,10)+" "+startM;String s3 = start.substring(0,10)+" "+endAf;Date dataMorning = HolidayDateUtil.format.parse(s2);Date dataAfternoon = HolidayDateUtil.format.parse(s3);Date dataCenter1 = HolidayDateUtil.format.parse(s);Date dataCenter2 = HolidayDateUtil.format.parse(s1);//开始时间在9点前 结束时间:①9点前 ②9点到12点 ③12点到1点 ④1点到6点 ⑤6点后if(dataStart.compareTo(dataMorning) <= 0){if(dataEnd.compareTo(dataMorning) <= 0){hours = 0;lunchBreak = 0;}else if(dataEnd.compareTo(dataMorning) > 0 && dataEnd.compareTo(dataCenter1) <= 0){hours = differentHoursByMillisecond(dataMorning,dataEnd);lunchBreak = 0;}else if(dataEnd.compareTo(dataCenter1) > 0 && dataEnd.compareTo(dataCenter2) <= 0){hours = differentHoursByMillisecond(dataMorning,dataCenter1);lunchBreak = 0;}else if(dataEnd.compareTo(dataCenter2) > 0 && dataEnd.compareTo(dataAfternoon) <= 0){hours = differentHoursByMillisecond(dataMorning,dataEnd);lunchBreak = 1;}else{hours = 8;lunchBreak = 0;}}//开始时间在9点到12点 结束时间:②9点到12点 ③12点到1点 ④1点到6点 ⑤6点后else if(dataStart.compareTo(dataMorning) > 0 && dataStart.compareTo(dataCenter1) <= 0){if(dataEnd.compareTo(dataMorning) > 0 && dataEnd.compareTo(dataCenter1) <= 0){hours = differentHoursByMillisecond(dataStart,dataEnd);lunchBreak = 0;}else if(dataEnd.compareTo(dataCenter1) > 0 && dataEnd.compareTo(dataCenter2) <= 0){hours = differentHoursByMillisecond(dataStart,dataEnd);lunchBreak = differentHoursByMillisecond(dataCenter1,dataEnd);}else if(dataEnd.compareTo(dataCenter2) > 0 && dataEnd.compareTo(dataAfternoon) <= 0){hours = differentHoursByMillisecond(dataStart,dataEnd);lunchBreak = 1;}else{hours = differentHoursByMillisecond(dataStart,dataAfternoon);lunchBreak = 1;}}//开始时间在12点到1点 结束时间:③12点到1点 ④1点到6点 ⑤6点后else if(dataStart.compareTo(dataCenter1) > 0 && (dataStart.compareTo(dataCenter2)) <= 0){if(dataEnd.compareTo(dataCenter1) > 0 && dataEnd.compareTo(dataCenter2) <= 0){hours = 0;lunchBreak = 0;}else if(dataEnd.compareTo(dataCenter2) > 0 && dataEnd.compareTo(dataAfternoon) <= 0){hours = differentHoursByMillisecond(dataCenter2,dataEnd);lunchBreak = 0;}else{hours = differentHoursByMillisecond(dataStart,dataAfternoon);lunchBreak = differentHoursByMillisecond(dataStart,dataCenter2);}}//开始时间在1点到6点 结束时间: ④1点到6点 ⑤6点后else if(dataStart.compareTo(dataCenter2) >0 && dataStart.compareTo(dataAfternoon) <= 0){if(dataEnd.compareTo(dataCenter2) > 0 && dataEnd.compareTo(dataAfternoon) <= 0){hours = differentHoursByMillisecond(dataStart,dataEnd);lunchBreak = 0;}else{hours = differentHoursByMillisecond(dataStart,dataAfternoon);lunchBreak = 0;}}//开始时间在6点后else{hours = 0;lunchBreak = 0;}}else{//间隔超过一天dataTime +=(i==0?0:1);if(dataTime>monthDays){mounthTime ++;dataTime=1;if(mounthTime>11){yearTime++;mounthTime=0;dataTime=1;}monthDays = HolidayDateUtil.daysMonth(yearTime,mounthTime);}//判断是否是节假日
//                        if(!"0".equals(HolidayDateUtil.getHoliday(yearTime+"-"+(((mounthTime+1))>=10?(mounthTime+1):"0"+(mounthTime+1))+"-"+(dataTime>10?dataTime:"0"+dataTime)))){
//                            System.out.println(""+yearTime+(((mounthTime+1))>10?(mounthTime+1):"0"+(mounthTime+1))+(dataTime>10?dataTime:"0"+dataTime)+"非工作日期不需要计算");
//                            continue;
//                        }String s1 = yearTime+"-"+(((mounthTime+1))>=10?(mounthTime+1):"0"+(mounthTime+1))+"-"+(dataTime>=10?dataTime:"0"+dataTime)+" "+(i==0?startday:startM);String s2 = yearTime+"-"+(((mounthTime+1))>=10?(mounthTime+1):"0"+(mounthTime+1))+"-"+(dataTime>=10?dataTime:"0"+dataTime)+" "+(i==interval?endday:endAf);Date dateMorning = HolidayDateUtil.format.parse(s1.substring(0,10)+" "+startM);Date dateAfternoon = HolidayDateUtil.format.parse(s1.substring(0,10)+" "+endAf);Date dateCenter1 = HolidayDateUtil.format.parse(s1.substring(0,10)+" "+endE);Date dateCenter2 = HolidayDateUtil.format.parse(s1.substring(0,10)+" "+startAf);dataStart = HolidayDateUtil.format.parse(s1);dataEnd = HolidayDateUtil.format.parse(s2);//第一天 dataEnd为6点 判断开始时间 ①9点前 ②9点到12点 ③12点到1点 ④ 1点到6点 ⑤ 6点之后if(i == 0){if(dataStart.compareTo(dateMorning) <= 0){hours = 8;lunchBreak = 0;}else if(dataStart .compareTo(dateMorning) > 0 && dataStart.compareTo(dateCenter1) <= 0){hours = differentHoursByMillisecond(dataStart,dataEnd);lunchBreak = 1;}else if(dataStart.compareTo(dateCenter1) > 0 && dataStart.compareTo(dateCenter2) <= 0){hours = differentHoursByMillisecond(dataStart,dataEnd);lunchBreak = differentHoursByMillisecond(dateCenter1,dataStart);}else if(dataStart.compareTo(dateCenter2) > 0 && dataStart.compareTo(dateAfternoon) <= 0){hours = differentHoursByMillisecond(dataStart,dataEnd);lunchBreak = 0;}else{hours = 0;lunchBreak = 0;}//中间天 hours 为8 lunchBreak为0}else if( i != 0 && i != interval){hours = 8;lunchBreak = 0;//最后一天 dataStart为9点 判断结束时间 ①9点前 ②9点到12点 ③12点到1点 ④ 1点到6点 ⑤ 6点之后}else{if(dataEnd.compareTo(dateMorning) <= 0){hours = 0;lunchBreak = 0;}else if(dataEnd .compareTo(dateMorning) > 0 && dataEnd.compareTo(dateCenter1) <= 0){hours = differentHoursByMillisecond(dataStart,dataEnd);lunchBreak = 0;}else if(dataEnd.compareTo(dateCenter1) > 0 && dataEnd.compareTo(dateCenter2) <= 0){hours = differentHoursByMillisecond(dataStart,dataEnd);lunchBreak = differentHoursByMillisecond(dateCenter1,dataEnd);}else if(dataEnd.compareTo(dateCenter2) > 0 && dataEnd.compareTo(dateAfternoon) <= 0){hours = differentHoursByMillisecond(dataStart,dataEnd);lunchBreak = 1;}else{hours = 8;lunchBreak = 0;}}}System.out.println(yearTime+"-"+(((mounthTime+1))>10?(mounthTime+1):"0"+(mounthTime+1))+"-"+(dataTime>10?dataTime:"0"+dataTime)+" 请假:"+(hours-lunchBreak)+"小时");totalDay+=hours-lunchBreak;}System.out.println(totalDay);return totalDay;} catch (ParseException e) {e.printStackTrace();}}return -1;}/*** 累积请假共多少天* @param totalDay* @return totalDay < 24小时按小时算   totalDay >= 24小时按天算*/public String totalDay(double totalDay){if(totalDay > 24){return (totalDay/24) + "天";}else{return totalDay + "小时";}}public static void main(String args[]) throws IOException{HolidayDateUtil qj= new HolidayDateUtil();//测试
//        qj.calculateTimeHour("2019-10-01 09:00:00","2019-10-03 12:00:00","08:00:00","11:30:00","13:00:00","17:30:00");
//       qj.calculateTimeHour("2019-10-01 07:30:00","2019-10-01 22:00:00","00:00:00","08:00:00","17:30:00","24:00:00");}
}

计算请假时间(不算节假日)相关推荐

  1. java 计算请假天数,去掉节假日

    计算请假天数,去掉节假日的 前端 function countDays() {//外出开始日期var startDate = new Date(DSF.getElementValueByKey(&qu ...

  2. java后台 计算请假时间(判断了周六周末和法定节假日和国家调休)

    目录 1.假设上班时间如下: 2.请假时间计算方式: 3.请假可能存在的情况: 4.实现思路: 5.代码奉上,只需传入时间参数 1.假设上班时间如下: 上班时间为:08:30:00 中午休息时间为:1 ...

  3. 去除法定节假日以及周末,计算请假时间

    业务需要,不废话,直接说核心. 直接上最佳解决方案,那就是动态的获取国家法定节假日(每年都会更新,可维护性更强).并去除周六周末. 当然,有一种比较low的解决方案就是把当年的法定节假日储存到表中或者 ...

  4. JS计算请假时间(起始时间与终止时间)

    正常上班时间为周一至周五 9:00 -12:00,13:00-18:00,实现了选择了请假起始时间与终止时间后,算出请假的总小时数,此总小时数扣除了非上班时间(周末,还有一天中的非上班时间:如午休的那 ...

  5. JS计算请假时间(起始时间与终止时间可跨月)

    正常上班时间为周一至周五 8:30 -12:00,13:30-18:00,实现了选择了请假起始时间与终止时间后,算出请假的总小时数,此总小时数扣除了非上班时间(周末,还有一天中的非上班时间:如午休的那 ...

  6. 判断请假时间去除周末及节假日

    公司业务需求,在请假时自动跳过周六周日以及节假日.通过statisticsOfLeaveTime方法,传入请假开始的时间以及请假结束的时间,则可自动统计调休时间. 代码整体逻辑就为拿到请假开始和结束时 ...

  7. Java计算请假时长(根据规则设置去除节假日、休息日、特殊日期)

    首先选择的日期要判断是不是节假日: 这里是写了工具类获取全年的日期信息. dateUtils工具类 某年第一天可以直接拼接 yyyy-01-01 获取节假日方法: public class DateU ...

  8. 计算请假工时,去除周六周末的时间

    做项目时,写到一个请假的模块,需要计算请假工时,去除周六周末,时间为一天8H,8:00-17:00,中午12:00-13:00 为午休时间 function getLeaveTime(startDat ...

  9. Python计算两日期之间排除节假日与非上班时间的工作时间

    Python计算两日期之间排除节假日与非上班时间的工作时间 前言 一.基本思路 二.代码示例 总结 文章目录 前言 一.基本思路 二.代码示例 总结 前言 工作中遇见需要写UDF计算事项办理时间的需求 ...

  10. 【工作日推算】JS计算当前时间前N个工作日(去除周末及节假日,文尾附源码下载)

    [写在前面]前些日子忙了几天有关指标对比分析的功能,因为系统是对接券商类的业务,所以他们比较关注的是工作日的数据波动,因此前端指标对比数据需要拿工作日的,不然他们停市的数据比较也没用,故而今天针对之前 ...

最新文章

  1. 算法基础知识科普:8大搜索算法之二叉搜索树(上)
  2. 《你的灯亮着吗》读书笔记3
  3. 腾讯AI击败王者荣耀职业队,全靠自学、策略清奇,一天训练量为人类440年
  4. 【Linux】目录文件权限的查看和修改【转】
  5. 指针:自定义函数length,调用它计算字符串的长度
  6. SELinux与SEAndroid
  7. SpringMVC-方法四种类型返回值总结,你用过几种?
  8. 随机邮箱_msgsafe - 一个处于半死不活状态的加密邮箱
  9. listview 模仿用户点击事件。
  10. python_10 迭代器和生成器
  11. 华为机试HJ40:统计字符
  12. 使用jQuery Mobile设计移动设备网站
  13. 2021年第十二届蓝桥杯javaA组省赛
  14. python 会议室预约系统解决方案_会议室预约管理系统方案书.pdf
  15. 肯德基中国门店将限时发售植物肉汉堡
  16. decorate装饰模式
  17. 【深度之眼PyTorch框架班第五期】作业打卡01:PyTorch简介及环境配置;PyTorch基础数据结构——张量
  18. python xlsx转换成txt
  19. volatile(粗浅理解)
  20. 寂然解读设计模式 - 单一职责原则

热门文章

  1. 神奇的夏时令——本来设置好的日期在保存完成后少了一天?
  2. matlab伽玛分布如何表示,matlab绘制gamma曲线
  3. Derek Sivers:砍掉一切没有惊讶感的内容(译)
  4. LeetCode | 521. Longest Uncommon Subsequence I
  5. php多级分栏,网页设计中分栏布局的几种实现方案
  6. java:打印1—100的数中有7和7的倍数
  7. win7如何修改html图标,Win7如何修改桌面图标
  8. android定时器课设报告,数显定时器课程设计报告.doc
  9. 愚你相遇,好幸运:遇见你,遇见了最好的自己
  10. 串口屏储存器不够,自己扩展怎么操作?