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

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

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));
    }

版权声明:本文为qq_37606901原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。