• 判断当前时间是否在某个时间段内


    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;
                }
            }
        }
    }
    
  • 相关阅读:
    Kibana安装
    25.Spring Cloud Sleuth与ELK
    Spring Cloud Sleuth综合整理
    26.Spring Cloud Sleuth与Zipkin
    算法与数据结构实验题 1.3 寻找幸运值
    算法与数据结构实验题 1.1 互质序列
    课程作业八
    课程作业七
    课程作业六
    课程作业五
  • 原文地址:https://www.cnblogs.com/Twittery/p/15355729.html
Copyright © 2020-2023  润新知