美文网首页
判断当前时间或者某一时间是否在一个时间段内(JAVA)

判断当前时间或者某一时间是否在一个时间段内(JAVA)

作者: EthanTseng | 来源:发表于2019-08-29 15:42 被阅读0次

    直接上代码, 这里要注意的是因为是用before和after方法, 所以要注意设置的时间区间问题. 因为格式化成的 "HH:mm" 原因, 所以这里, 哪怕是14:57:59秒, ("14:57:59").after("14:57") 也是 false 的, 故before也是.

       /**
         * 判断当前时间是否在14:57-15:00之间
         *
         * @return boolean
         */
        public static boolean judgeIsSetBiddingTime() {
            //设置日期格式
            SimpleDateFormat df = new SimpleDateFormat("HH:mm");
            Date now = null;
            Date beginTime = null;
            Date endTime = null;
    
            try {
                now = df.parse(df.format(new Date()));
                beginTime = df.parse("14:56");
                endTime = df.parse("15:01");
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            Calendar date = Calendar.getInstance();
            date.setTime(now);
    
            Calendar begin = Calendar.getInstance();
            begin.setTime(beginTime);
    
            Calendar end = Calendar.getInstance();
            end.setTime(endTime);
    
            return date.after(begin) && date.before(end);
        }
    

    相关文章

      网友评论

          本文标题:判断当前时间或者某一时间是否在一个时间段内(JAVA)

          本文链接:https://www.haomeiwen.com/subject/gaviectx.html