欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

小记:判断当前时间是否在某一个时间范围内

程序员文章站 2022-03-11 22:29:12
...

时间范围内的判断,只要传入相应的参数即可:

public static boolean between(Date now, Date start, Date end) {
        if (null == now || null == start || null == end) {
            return false;
        }
        return now.compareTo(start) >= 0 && now.compareTo(end) < 0;
    }

其中compareTo()是java.util.Date下的一个比较时间的工具

 public int compareTo(Date anotherDate) {
        long thisTime = getMillisOf(this);
        long anotherTime = getMillisOf(anotherDate);
        return (thisTime<anotherTime ? -1 : (thisTime==anotherTime ? 0 : 1));
    }
相关标签: 小记