/**
* 判断当前系统时间是否在指定时间的范围内
*
* @param beginHour
* 开始小时,例如22
* @param beginMin
* 开始小时的分钟数,例如30
* @param endHour
* 结束小时,例如 8
* @param endMin
* 结束小时的分钟数,例如0
* @return true表示在范围内,否则false
*/
public static boolean isCurrentInTimeScope(int beginHour, int beginMin, int endHour, int endMin) {
boolean result = false;
final long aDayInMillis = 1000 * 60 * 60 * 24;
final long currentTimeMillis = System.currentTimeMillis();
Time now = new Time();
now.set(currentTimeMillis);
Time startTime = new Time();
startTime.set(currentTimeMillis);
startTime.hour = beginHour;
startTime.minute = beginMin;
Time endTime = new Time();
endTime.set(currentTimeMillis);
endTime.hour = endHour;
endTime.minute = endMin;
if (!startTime.before(endTime)) {
// 跨天的特殊情况(比如22:00-8:00)
startTime.set(startTime.toMillis(true) - aDayInMillis);
result = !now.before(startTime) && !now.after(endTime); // startTime <= now <= endTime
Time startTimeInThisDay = new Time();
startTimeInThisDay.set(startTime.toMillis(true) + aDayInMillis);
if (!now.before(startTimeInThisDay)) {
result = true;
}
} else {
// 普通情况(比如 8:00 - 14:00)
result = !now.before(startTime) && !now.after(endTime); // startTime <= now <= endTime
}
return result;
}