如何基于java.util.Date判断当前时间是否在两个时间点之间(忽略日期)
解决Java中忽略年月日仅比较时分秒的时间区间判断问题
没问题,我来帮你搞定这个只比较时分秒的时间区间判断需求~核心思路就是把所有时间的年月日信息剥离,只保留时分秒部分,再进行区间对比。下面给你两种实用的实现方案,分别兼容旧Java版本和推荐的新API:
方案一:用传统Calendar类(兼容Java 7及以下)
如果你的项目还在使用旧版Java,用Calendar类可以轻松剥离年月日:
import java.util.Calendar; import java.util.Date; public class TimeRangeChecker { public static boolean isCurrentTimeInRange(Date startTime, Date endTime) { Date currentTime = new Date(); // 初始化Calendar实例,分别处理三个时间 Calendar startCal = Calendar.getInstance(); startCal.setTime(startTime); Calendar endCal = Calendar.getInstance(); endCal.setTime(endTime); Calendar currentCal = Calendar.getInstance(); currentCal.setTime(currentTime); // 将三个时间的年月日统一设为同一天(比如1970-01-01),这样就只保留时分秒的差异 normalizeCalendarDate(startCal); normalizeCalendarDate(endCal); normalizeCalendarDate(currentCal); // 转换回Date对象,此时仅包含时分秒信息 Date normalizedStart = startCal.getTime(); Date normalizedEnd = endCal.getTime(); Date normalizedCurrent = currentCal.getTime(); // 处理跨天的特殊情况(比如起始时间是22:00,结束时间是06:00) if (normalizedStart.after(normalizedEnd)) { return normalizedCurrent.after(normalizedStart) || normalizedCurrent.before(normalizedEnd); } else { // 正常区间:当前时间在起始和结束之间(包含边界值) return !normalizedCurrent.before(normalizedStart) && !normalizedCurrent.after(normalizedEnd); } } // 抽取统一设置年月日的方法,避免重复代码 private static void normalizeCalendarDate(Calendar cal) { cal.set(Calendar.YEAR, 1970); cal.set(Calendar.MONTH, Calendar.JANUARY); cal.set(Calendar.DAY_OF_MONTH, 1); } // 测试示例 public static void main(String[] args) throws Exception { // 模拟示例中的时间参数 Date date1 = new java.text.SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2008/11/10 08:05:55"); Date date2 = new java.text.SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2010/12/11 10:12:33"); // 模拟当前时间 Date current = new java.text.SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2020/09/29 09:12:13"); boolean result = isCurrentTimeInRange(date1, date2); System.out.println(result); // 输出:true,符合预期 } }
方案二:用Java 8+的java.time API(推荐)
java.util.Date是过时的API,Java 8引入的LocalTime专门用来处理时分秒,代码会更简洁直观:
import java.time.LocalTime; import java.time.ZoneId; import java.util.Date; public class TimeRangeChecker { public static boolean isCurrentTimeInRange(Date startTime, Date endTime) { // 将Date转换为LocalTime,自动剥离年月日信息 LocalTime start = startTime.toInstant().atZone(ZoneId.systemDefault()).toLocalTime(); LocalTime end = endTime.toInstant().atZone(ZoneId.systemDefault()).toLocalTime(); LocalTime current = LocalTime.now(); // 处理跨天情况 if (start.isAfter(end)) { return current.isAfter(start) || current.isBefore(end); } else { // 正常区间判断,包含边界值 return current.compareTo(start) >= 0 && current.compareTo(end) <= 0; } } // 测试示例 public static void main(String[] args) throws Exception { Date date1 = new java.text.SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2008/11/10 08:05:55"); Date date2 = new java.text.SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2010/12/11 10:12:33"); boolean result = isCurrentTimeInRange(date1, date2); System.out.println(result); // 输出:true } }
注意事项
- 时区问题:上面的代码使用系统默认时区,如果业务需要固定时区(比如UTC),请把
ZoneId.systemDefault()替换为ZoneId.of("UTC")或对应的时区ID。 - 跨天场景:两种方案都考虑了起始时间晚于结束时间的跨天情况(比如晚上10点到次日早上6点),这种场景下只要当前时间在起始时间之后,或者结束时间之前,都会返回true。
- 边界值处理:代码中默认包含等于起始/结束时间的情况,如果不需要包含,只需修改判断条件(比如把
>=改成>,<=改成<)。
内容的提问来源于stack exchange,提问作者lucas.iter
相关产品推荐
相关产品推荐

