Java:如何检查日期是否在一定范围内? [英] Java: how do I check if a Date is within a certain range?
本文介绍了Java:如何检查日期是否在一定范围内?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一系列包含开始日期和结束日期的范围.我想检查日期是否在该范围内.
I have a series of ranges with start dates and end dates. I want to check to see if a date is within that range.
Date.before() 和 Date.after() 使用起来好像有点别扭.我真正需要的是这样的伪代码:
Date.before() and Date.after() seem to be a little awkward to use. What I really need is something like this pseudocode:
boolean isWithinRange(Date testDate) {
return testDate >= startDate && testDate <= endDate;
}
不确定它是否相关,但我从数据库中提取的日期有时间戳.
Not sure if it's relevant, but the dates I'm pulling from the database have timestamps.
推荐答案
boolean isWithinRange(Date testDate) {
return !(testDate.before(startDate) || testDate.after(endDate));
}
我觉得没那么尴尬.请注意,我是这样写的,而不是
Doesn't seem that awkward to me. Note that I wrote it that way instead of
return testDate.after(startDate) && testDate.before(endDate);
所以即使 testDate 正好等于最终情况之一,它也会起作用.
so it would work even if testDate was exactly equal to one of the end cases.
这篇关于Java:如何检查日期是否在一定范围内?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文