0

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

  • 5
    **Don't use `java.util.Date`**, instead **use `java.time.OffsetDateTime` or `java.time.ZonedDateTime` or `java.time.LocalDate`**... And for the check, find their methods `isBefore()` and `isAfter()`. – deHaar Aug 17 '20 at 08:19
  • 2
    On top of deHaar said, are you sure your conditions are correct? Like shouldn't `fromDate.after(s.getStartDate())` be changed to `fromDate.before(s.getStartDate())` and same for the second? – Amongalen Aug 17 '20 at 08:29
  • if startDate is 07/11/2018, valid fromDates are all dates after this date or equal to it, 08/11;09/12; etc. so, if i enter 09/09/2019-09/09/2020 it should return this event in that range @Amongalen –  Aug 17 '20 at 08:40
  • i updated my question, i hope is more clear now what I am looking for –  Aug 17 '20 at 08:46
  • Do you require your event to be *completely within* the range from `fromDate` to `toDate`, or is it enough that it overlaps (some of the time falls within the range)? – Ole V.V. Aug 17 '20 at 19:03

2 Answers2

0

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
}

Traycho Ivanov
  • 2,887
  • 14
  • 24
0

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) >= 0 && d.before(endExclusive);
}

yielding

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)
}

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.

tucuxi
  • 17,561
  • 2
  • 43
  • 74