7

ok not as simple as title may make it sound. I tried this in a very primal way with c# and it worked, but I have a feeling a better job could be achieved with Java and Oracle as database. So the thing is:

I have a reservation system. multiple bookings could be made on the same day for period between date X and date Y as long as each day in the range can accommodate the requested number. Maximum number of clusters to reserve is 46. Hence logically you would look at each day as a holder of 46 cluster reservation and deduce from that.

Now what I have difficulty working out is:

when there are n number of bookings stored and valid in database, then I want to make new booking. So how do I check if this new date range falls within any of the previously booked days or not. Not talking simply here about x falling in y (as ranges). More like:

   X_______________________________________Y
       X________________________________y
  X________________________________Y
                         X________________________________Y

as u can see the overlap is happening.

Please let me know how could I do this as it will affect early design of objects Regards

j08691
  • 204,283
  • 31
  • 260
  • 272
sys_debug
  • 3,883
  • 17
  • 67
  • 98
  • possible duplicate of [Determine Whether Two Date Ranges Overlap](http://stackoverflow.com/questions/325933/determine-whether-two-date-ranges-overlap) – Basil Bourque May 03 '15 at 22:20

2 Answers2

8

Assume your date has two methods: isBefore(Date other) and isAfter(Date other). Obviously if they don't you can cure this with an external method or wrapping or something. Edit: java.util.Date has compareTo method you could use.

You do this:

public boolean overlapsWithExisting(Booking booking) {
    final Date early = booking.getStart();
    final Date late = booking.getEnd();
    for(Booking existing : existingBookings) {
        if(!(early.isAfter(existing.getEnd()) || late.isBefore(existing.getStart()))
            return true;
    }
    return false;
}

We compare this booking to all existing bookings. If this booking ends before the existing booking even starts, or if this booking starts after the existing booking ends, then it doesn't conflict. Any other condition and they will overlap.

Do this to each booking.

Mahmoud Saleh
  • 33,303
  • 119
  • 337
  • 498
corsiKa
  • 81,495
  • 25
  • 153
  • 204
  • Nice nice...could u just elaborate more on what u encapsulated or meant by Booking? because u creating instances here – sys_debug Nov 06 '11 at 22:02
  • Well I'm assuming your `Booking` has a method to get the start and end dates of the booking. And it doesn't just work for dates, if you had hours or minutes or whatever that works too. I'm not sure what your follow question really is. My `Booking` object just represents what you mentioned about having bookings, as I assume it has an object in your Java code too. – corsiKa Nov 06 '11 at 22:06
  • not yet as of now...but soon the classes will be created. But I guess I got the hint. so just receive the parameters (user input in this case) and then deal with them. Thanks a lot! – sys_debug Nov 06 '11 at 22:08
  • @corsiKa, thanks for sharing this great solution, it really saved me lots of time. – Mahmoud Saleh Dec 18 '12 at 12:07
1

Joda-Time – Interval

Rather than roll your own, use the Interval class in the Joda-Time library. An Interval is a pair of specific points along the timeline, each defined as a DateTime instance.

The Interval class offers overlap, gap, and abuts methods.

Half-Open

Those methods wisely use the Half-Open approach to spans of time where the beginning is inclusive while the ending is exclusive. Search StackOverflow for more info.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154