zoukankan      html  css  js  c++  java
  • 判断当前时间是否在某个时间段内

    package com.xx.xxx.xxx.xxx;
    
    import java.text.DateFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    
    public class test {
    
        public static void main(String[] args) throws ParseException {
            String format = "HH:mm:ss";
    
            Date nowStr = new SimpleDateFormat(format).parse("09:26:00");
    
            Date now = new Date();
            DateFormat dateFormat = DateFormat.getTimeInstance();//获取时分秒//得到当前时间的时分秒String nowStr = dateFormat.format(now);
    
            Date nowTime = new SimpleDateFormat(format).parse(dateFormat.format(nowStr));
    
            Date startTime = new SimpleDateFormat(format).parse("09:27:00");
            Date endTime = new SimpleDateFormat(format).parse("09:27:59");
    
    
            System.out.println(isEffectiveDate(nowTime, startTime, endTime));
    
        }
    
        /**
         * 判断当前时间是否在[startTime, endTime]区间,注意时间格式要一致
         *
         * @param nowTime   当前时间
         * @param startTime 开始时间
         * @param endTime   结束时间
         * @return
         * @author jqlin
         */
        public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
            if (nowTime.getTime() == startTime.getTime()
                    || nowTime.getTime() == endTime.getTime()) {
                return true;
            }
    
            Calendar date = Calendar.getInstance();
            date.setTime(nowTime);
    
            Calendar begin = Calendar.getInstance();
            begin.setTime(startTime);
    
            Calendar end = Calendar.getInstance();
            end.setTime(endTime);
    
            if (date.after(begin) && date.before(end)) {
                return true;
            } else {
                return false;
            }
        }
    
        public static void Effective() {
            // 判断当前时间是否包含在页面活动创建时间、页面活动接口时间之内。
            Date currentTime = new Date();
            Date startValidTime = config.getStartValidTime();
            Date endValidTime = config.getEndValidTime();
            log.info("当前时间:{},页面生效时间:{},页面失效时间:{}", currentTime, startValidTime, endValidTime);
            if (startValidTime != null && endValidTime != null) {
                if (!(currentTime.after(startValidTime) && currentTime.before(endValidTime))) {
                    return null;
                }
            }
        }
    }
    
  • 相关阅读:
    Java实现 蓝桥杯VIP 算法提高 P0404
    Java实现 蓝桥杯VIP 算法提高 P0404
    Java实现 蓝桥杯VIP 算法提高 P0404
    Java实现 蓝桥杯VIP 算法提高 P0404
    Java实现 蓝桥杯VIP 算法提高 P0404
    Java实现 蓝桥杯VIP 算法训练 排列问题
    Java实现 蓝桥杯VIP 算法训练 排列问题
    Java实现 蓝桥杯VIP 算法训练 排列问题
    Java实现 蓝桥杯VIP 算法训练 排列问题
    关于模态/非模态对话框不响应菜单的UPDATE_COMMAND_UI消息(对对WM_INITMENUPOPUP消息的处理)
  • 原文地址:https://www.cnblogs.com/Twittery/p/15355729.html
Copyright © 2011-2022 走看看