java判断当前时间在某个时间段内

时间:2025-04-10 12:09:21
public class test4 {
	public static void main(String[] args) {
		SimpleDateFormat df = new SimpleDateFormat("HH:mm");// 设置日期格式
		Date now = null;
		Date beginTime = null;
		Date endTime = null;
		try {
			now = ((new Date()));
			beginTime = ("06:00");
			endTime = ("22:00");
		} catch (Exception e) {
			();
		}
		Boolean flag = belongCalendar(now, beginTime, endTime);
		(flag);
	}

	/**
	 * 判断时间是否在时间段内
	 * 
	 * @param nowTime
	 * @param beginTime
	 * @param endTime
	 * @return
	 */
	public static boolean belongCalendar(Date nowTime, Date beginTime,
			Date endTime) {
		Calendar date = ();
		(nowTime);

		Calendar begin = ();
		(beginTime);

		Calendar end = ();
		(endTime);

		if ((begin) && (end)) {
			return true;
		} else {
			return false;
		}
	}
}