二话不说直接上代码
`/** * 判断当前系统时间是否在指定时间的范围内 * * beginHour 开始小时,例如22 * beginMin 开始小时的分钟数,例如30 * endHour 结束小时,例如 8 * endMin 结束小时的分钟数,例如0 * 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; // 跨天的特殊情况(比如22:00-8: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 { //普通情况(比如 8:00 - 14:00) result = !now.before(startTime) && !now.after(endTime); // startTime <= now <= endTime } return result;}`