zoukankan      html  css  js  c++  java
  • 根据开始时间结束时间,计算工作日的天数

    整理了一下,感觉第三方的接口不是很稳定,欢迎提供新的接口地址

      

    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.time.LocalDate;
    import java.time.LocalDateTime;
    import java.time.format.DateTimeFormatter;
    
    /**
     * @author: DevanYan
     * @create: 2019-06-10 10:52
     */
    
    //工作时长计算工具
    public class WorkDayUtil {
    
    
    
        private static final String API_URL = "http://api.goseek.cn/Tools/holiday?date=";
        private static final int  WORK_STARK = 9;
        private static final int  WORK_STOP = 18;
    
        /**
         *
         * @param beginDateTime 开始时间
         * @param endDateTime 结束时间
         * @return 时长(单位:天,最小计数单位:0.5)
         */
        public static double workDays(LocalDateTime beginDateTime,LocalDateTime endDateTime){
    
            double workDays = 0;
            LocalDate beginDate = LocalDate.of(beginDateTime.getYear(),beginDateTime.getMonth(),beginDateTime.getDayOfMonth());
            LocalDate endDate = LocalDate.of(endDateTime.getYear(),endDateTime.getMonth(),endDateTime.getDayOfMonth());
            Long days =Math.abs(beginDate.toEpochDay()-endDate.toEpochDay());
    
            int beginHour = Integer.valueOf(beginDateTime.format(DateTimeFormatter.ofPattern("HH")));
            int endHour = Integer.valueOf(endDateTime.format(DateTimeFormatter.ofPattern("HH")));
    
            if( beginDate.toString().equals(endDate.toString()) ){//同一天
                if(WorkDayUtil.isWorkDay(beginDate)){
                    if( ( beginHour>= WORK_STARK && endHour >= WORK_STARK && beginHour <= 12 && endHour <= 12) || ( beginHour > 12 && endHour > 12 && beginHour <= WORK_STOP && endHour <= WORK_STOP) ){
                        workDays = 0.5;
                    }else{
                        workDays = 1;
                    }
                }else{
                    workDays = 0;
                }
    
            }else{//跨天
                LocalDate dealDate = beginDate;
                for(int i = 0;i<days-1;i++){
                    dealDate = dealDate.minusDays(-1);
                    if(WorkDayUtil.isWorkDay(dealDate)){
                        workDays++;
                    }
                }
                //第一天
                if(WorkDayUtil.isWorkDay(beginDate)){
                   if(beginHour>12 ){
                       if(beginHour <= WORK_STOP){
                           workDays = workDays + 0.5;
                       }
                   }else{
                       workDays ++;
                   }
                }
                //末天
                if(WorkDayUtil.isWorkDay(endDate)){
                    if(endHour <= 12){
                        workDays = workDays + 0.5;
                    }else{
                        workDays ++;
                    }
                }
            }
            return workDays;
        }
    
    
        public static boolean isWorkDay(LocalDate date){
            String dayStr = date.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
            int flag = WorkDayUtil.apiGet(dayStr);
            if(flag == 0 || flag == 2){
                return true;
            }
            return false;
        }
    
        /**
         * @param dateStr
         * @return 正常工作日对应结果为 0, 法定节假日对应结果为 1, 节假日调休补班对应的结果为 2,休息日对应结果为 3
         */
        public static int apiGet( String dateStr) {
            String httpUrl= API_URL + dateStr;
            BufferedReader reader = null;
            String result = null;
            StringBuffer sbf = new StringBuffer();
            int d = 0;
            try {
                URL url = new URL(httpUrl);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                connection.connect();
                InputStream is = connection.getInputStream();
                reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));String strRead = null;
                while ((strRead = reader.readLine()) != null) {
                    sbf.append(strRead);
                }
                reader.close();
                result = sbf.toString();
                result = result.trim();
                System.out.println(result);
                if(result.indexOf("10000")!=-1){
                    result = result.substring(result.indexOf(":")+1);
                    result = result.substring(result.indexOf(":")+1);
                    result = result.substring(0,result.length()-1);
                    d = Integer.valueOf(result);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return d;
        }
    
        public static void main(String[] args) {
            LocalDateTime begin = LocalDateTime.parse("2019-06-01 09:00:00",DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            LocalDateTime end = LocalDateTime.parse("2019-06-14 11:00:00",DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            System.out.println("工作日:"+WorkDayUtil.workDays(begin,end) +"天");
    
        }
    
    
    
    }
  • 相关阅读:
    狗 日 的 360
    Django搭建简单的站点
    ZOJ 3675 Trim the Nails(bfs)
    Qt移动应用开发(二):使用动画框架
    SPOJ QTREE2 lct
    [Phonegap+Sencha Touch] 移动开发77 Cordova Hot Code Push插件实现自己主动更新App的Web内容
    Bitmap工具类BitmapHelper
    Material-design icon生成插件
    闲聊ROOT权限——ROOT权限的前世今生
    Java深入浅出系列(四)——深入剖析动态代理--从静态代理到动态代理的演化
  • 原文地址:https://www.cnblogs.com/devan/p/10997358.html
Copyright © 2011-2022 走看看