參考鏈接: https://www.jianshu.com/p/4497b53d3c94 /***
*
* @param beginHour 開始小時 比如 23
* @param beginMin 開始小時的分鐘 比如 00
* @param endHour 結(jié)束小時 比如 5
* @param endMin 結(jié)束小時的分鐘 比如 00
* @return true表示范圍內(nèi) 否則false
*/
public static boolean atTheCurrentTime(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;
/**跨天的特殊情況(比如23:00-2:00)*/
if (!startTime.before(endTime)) {
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 {
/**普通情況(比如5:00-10:00)*/
result = !now.before(startTime) && !now.after(endTime); // startTime <= now <= endTime
}
return result;
}
|