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) +"天");
    
        }
    
    
    
    }
  • 相关阅读:
    PCB设计实战项目指导班26层板的设计
    AT2171 [AGC007D] Shik and Game 题解
    UVA11327 Enumerating Rational Numbers 题解
    P6222 「P6156 简单题」加强版 题解
    CF702F TShirts 题解
    P3747 [六省联考 2017] 相逢是问候 题解
    『学习笔记』PollardRho 算法 题解
    P7453 [THUSCH2017] 大魔法师 题解
    CF1575L Longest Array Deconstruction 题解
    最大公约数之和
  • 原文地址:https://www.cnblogs.com/devan/p/10997358.html
Copyright © 2011-2022 走看看