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;
                }
            }
        }
    }
    
  • 相关阅读:
    Windows环境下Unicode编程总结
    我的CS脚本autoexec.cfg
    完成端口与高性能服务器程序开发[引用]
    调用未知DLL中的导出函数
    兼容Vista 赛门铁克公测新杀毒软件
    I Love You的真正含义
    码根码
    木马经典十大藏身地点大搜查
    Windows调试器及不同平台符号包下载地址(收集)
    “千般路”与“磨豆腐”
  • 原文地址:https://www.cnblogs.com/Twittery/p/15355729.html
Copyright © 2011-2022 走看看