JAVA judges that the current time is within the time range

We must have some functions around time selection in our daily development,

Today, I will share with you a small demo of how java determines whether the current time is within the selected time range

	public static void main(String[] args) throws ParseException {
		SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss");
		Date startTime = ft.parse("2019-06-05 03:26:54");
		Date endTime = ft.parse("2019-06-09 03:26:54");
		Date nowTime = new Date();
		boolean effectiveDate = isEffectiveDate(nowTime, startTime, endTime);
		if (effectiveDate) {
			System.out.println("当前时间在范围内");
		}else {
			System.out.println("当前时间在不在范围内");
		}
		
	}
	/**
	 * 
	 * @param nowTime   当前时间	
	 * @param startTime	开始时间
	 * @param endTime   结束时间
	 * @return
	 * @author sunran   判断当前时间在时间区间内
	 */
	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;
        }
    }

current time

insert image description here

So it's in range

insert image description here

Guess you like

Origin blog.csdn.net/m0_54883970/article/details/124270081