【MapSheep】
[好记性不如烂笔头]
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;
            }
        }
    }
}
posted on 2021-09-30 10:50  (Play)  阅读(704)  评论(0编辑  收藏  举报