java代码中判断一个时间是否在某一个时间段的范围内

本文介绍了一种用于判断特定时间(小时和分钟级别)是否位于给定时间区间内的实用方法。该方法采用SimpleDateFormat进行时间格式化,并通过解析开始时间、结束时间和当前时间来实现区间判断。适用于需要对时间进行精确控制的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
 * 
 * @param nowDate   要比较的时间
 * @param startDate   开始时间
 * @param endDate   结束时间
 * @return   true在时间段内,false不在时间段内
 * @throws Exception
 */
public static boolean hourMinuteBetween(String nowDate, String startDate, String endDate) throws Exception{
	
	SimpleDateFormat format = new SimpleDateFormat("HH:mm");
	
	Date now = format.parse(nowDate);
	Date start = format.parse(startDate);
	Date end = format.parse(endDate);
	
	long nowTime = now.getTime();
	long startTime = start.getTime();
	long endTime = end.getTime();
	
	return nowTime >= startTime && nowTime <= endTime;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值