Debemos tener algunas funciones en torno a la selección del tiempo en nuestro desarrollo diario,
Hoy compartiré con ustedes una pequeña demostración de cómo Java determina si la hora actual está dentro del rango de tiempo seleccionado.
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;
}
}
tiempo actual
Entonces está dentro del rango