用Java按照startDate和endDate进行日期筛选。

huangapple go评论79阅读模式
英文:

Filter by date in Java with startDate and endDate

问题

在数据库中,我有

eventName: "TestEvent",
startDate: ISODate("2018-11-07T13:24:03.124Z"),
endDate: ISODate("2020-11-07T13:24:03.124Z")

我正在设置两个日期 fromDatetoDate,例如

fromDate: 01/01/2020
toDate: 01/01/2021

我想要检查事件是否在我输入的这个范围内。
我尝试了以下方式,但结果不正确。

if ((fromDate.after(s.getStartDate()) && toDate.before(s.getEndDate()))
|| s.getStartDate().equals(fromDate) || s.getEndDate().equals(toDate))

请帮助我,我在项目中使用 utils.Date。

如果我使用:

fromDate: 08/08/2017
toDate: 09/09/2022

或者

fromDate: 08/08/2019
toDate: 09/09/2019

它应该在这个范围内返回该事件。

如果我使用:

fromDate: 01/01/2000
toDate: 01/01/2001

该事件不应出现在结果中。

英文:

In database I have

eventName: "TestEvent",
startDate : ISODate("2018-11-07T13:24:03.124Z"),
endDate: ISODate("2020-11-07T13:24:03.124Z")

I am setting two dates fromDate and toDate,
for example

fromDate:01/01/2020
toDate:01/01/2021

I want to check if the event is within this range I entered.
I tried like this but the results are not correct.

if ((fromDate.after(s.getStartDate()) && toDate.before(s.getEndDate()))
|| s.getStartDate().equals(fromDate) || s.getEndDate().equals(toDate))

Please help me, I am using utils.Date in my project.

If i use :

fromDate:08/08/2017
toDate: 09/09/2022

or

fromDate:08/08/2019
toDate: 09/09/2019

it should return this event in this range.

If i use :

fromDate:01/01/2000
toDate: 01/01/2001

this event should not be in the results

答案1

得分: 0

确保在比较日期时它们的格式相同。

我认为条件存在问题,应该更像这样:

Date startDate = s.getStartDate();
Date endDate  = s.getEndDate();

if ((fromDate.before(startDate) || fromDate.equals(startDate)) &&
    (toDate.after(endDate) || toDate.equals(endDate))){

   // 在这里进行操作
}

英文:

Make sure when you compare the dates they are in the same format.

I think there is an issue with condition and it should look more like :

Date startDate = s.getStartDate();
Date endDate  = s.getEndDate();

if ((fromDate.before(startDate) || fromDate.equals(startDate)) && 
    (toDate.after(endDate) || toDate.equals(endDate)){

   // do you magic here
}

答案2

得分: 0

你正在尝试检查两个(时间)间隔是否重叠。一个简单的方法是,只有在以下情况下重叠才会发生:第一个间隔的起始和结束之一在第二个间隔内,或者反过来。通过以下方式进行测试:

boolean contains(Date d, Date startInclusive, Date endExclusive) {
    return d.compareTo(startInclusive) >= 0 && d.before(endExclusive);
}

得到结果:

boolean intersects(Date from, Date to, Date start, Date end) {
    return contains(from, start, end) || // s <= f < e (from inside start-end)
           contains(to, start, end) ||   // s <= t < e (to inside start-end)
           contains(start, from, to);    // f <= s < t (from-to fully contains start-end)
}

注意,这比编写(等效的)布尔表达式要可读得多。

我还完全同意@deHaar的评论:在Java中使用java.time中的类,而不是java.util.Date。java.util.Date存在许多问题,从Java 8开始已被弃用。

英文:

You are trying to check whether two (time) intervals overlap. A simple way to look at it is to say that overlap occurs if (and only if) one or more of the start and end of the 1st interval is within the 2nd, or vice-versa. Test it with:

boolean contains(Date d, Date startInclusive, Date endExclusive) {
     return d.compareTo(startInclusive) &gt;= 0 &amp;&amp; d.before(endExclusive);
}

yielding

boolean intersects(Date from, Date to, Date start, Date end) {
     return contains(from, start, end) || // s &lt;= f &lt; e (from inside start-end)
            contains(to, start, end) ||   // s &lt;= t &lt; e (to inside start-end)
            contains(start, from, to);    // f &lt;= s &lt; t (from-to fully contains start-end)
}

Note that this is much more readable than writing the (equivalent) boolean expression.

I also fully agree with @deHaar's comment: use classes in java.time instead of java.util.Date. There are many problems with java.util.Date, which is deprecated as of Java 8.

huangapple
  • 本文由 发表于 2020年8月17日 16:18:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/63447098.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定