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