zoukankan      html  css  js  c++  java
  • 判断某个时间点在一个时间段内方法汇总

    方法一

     /**
         * @param strDate      需要判断的时间
         * @param strDateBegin 开始时间
         * @param strDateEnd   结束时间
         * @return
         */
        public boolean isInDates(String strDate, String strDateBegin, String strDateEnd) {
            SimpleDateFormat sd = new SimpleDateFormat("HH:mm:ss");
            Date myDate = null;
            Date dateBegin = null;
            Date dateEnd = null;
            try {
                myDate = sd.parse(strDate);
                dateBegin = sd.parse(strDateBegin);
                dateEnd = sd.parse(strDateEnd);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            strDate = String.valueOf(myDate);
            strDateBegin = String.valueOf(dateBegin);
            strDateEnd = String.valueOf(dateEnd);
    
            int strDateH = Integer.parseInt(strDate.substring(11, 13));
            int strDateM = Integer.parseInt(strDate.substring(14, 16));
            int strDateS = Integer.parseInt(strDate.substring(17, 19));
    
            int strDateBeginH = Integer.parseInt(strDateBegin.substring(11, 13));
            int strDateBeginM = Integer.parseInt(strDateBegin.substring(14, 16));
            int strDateBeginS = Integer.parseInt(strDateBegin.substring(17, 19));
    
            int strDateEndH = Integer.parseInt(strDateEnd.substring(11, 13));
            int strDateEndM = Integer.parseInt(strDateEnd.substring(14, 16));
            int strDateEndS = Integer.parseInt(strDateEnd.substring(17, 19));
    
            if ((strDateH >= strDateBeginH && strDateH <= strDateEndH)) {
                if (strDateH > strDateBeginH && strDateH < strDateEndH) {
                    return true;
                } else if (strDateH == strDateBeginH && strDateM > strDateBeginM && strDateH < strDateEndH) {
                    return true;
                } else if (strDateH == strDateBeginH && strDateM == strDateBeginM && strDateS > strDateBeginS && strDateH < strDateEndH) {
                    return true;
                } else if (strDateH == strDateBeginH && strDateM == strDateBeginM && strDateS == strDateBeginS && strDateH < strDateEndH) {
                    return true;
                } else if (strDateH > strDateBeginH && strDateH == strDateEndH && strDateM < strDateEndM) {
                    return true;
                } else if (strDateH > strDateBeginH && strDateH == strDateEndH && strDateM == strDateEndM && strDateS < strDateEndS) {
                    return true;
                } else if (strDateH > strDateBeginH && strDateH == strDateEndH && strDateM == strDateEndM && strDateS == strDateEndS) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }

    方法二

                         //需要判断的时间
                        Calendar date = Calendar.getInstance();
                        date.setTime(判断的时间);
                        //开始时间
                        Calendar begin = Calendar.getInstance();
                        begin.setTime(开始时间);
                        //结束时间
                        Calendar end = Calendar.getInstance();
                        end.setTime(结束时间);
                        boolean flag = date.after(begin) && date.before(end);             
  • 相关阅读:
    反转链表 16
    CodeForces 701A Cards
    hdu 1087 Super Jumping! Jumping! Jumping!(动态规划)
    hdu 1241 Oil Deposits(水一发,自我的DFS)
    CodeForces 703B(容斥定理)
    poj 1067 取石子游戏(威佐夫博奕(Wythoff Game))
    ACM 马拦过河卒(动态规划)
    hdu 1005 Number Sequence
    51nod 1170 1770 数数字(数学技巧)
    hdu 2160 母猪的故事(睡前随机水一发)(斐波那契数列)
  • 原文地址:https://www.cnblogs.com/ymxl/p/13322641.html
Copyright © 2011-2022 走看看