判断指定时间是否在指定时间范围

发布时间 2023-11-18 15:31:26作者: 岁月记忆
    /**
     * 判断指定时间是否在指定时间范围
     * 指定时间为 null 时, 指定时间为 当前时间
     * @param from 开始时间
     * @param to   结束时间
     * @return 结果 当  from ≥ 当前时间 ≤ to :true,否则 false
     */
    public static boolean between(Date from, Date to,Date now) {
        if (from == null) {
            throw new IllegalArgumentException("开始时间不能为空");
        }
        if (to == null) {
            throw new IllegalArgumentException("结束时间不能为空");
        }
        if(now == null){
            now = new Date();
        }
        return !(now.after(to)) && !(now.before(from));
    }