0

I am working on a project for booking venues. The aim is to not allow the booking if the venue is already booking during this period

 if (!alreadyBookedSet.isEmpty()) {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");

        logger.debug("Check if the venue is already booked on this date" + booking.getBookingDate());
        for (Booking alreadyBooked : alreadyBookedSet) {
            String bookingDate = alreadyBooked.getBookingDate();

            if (bookingDate.equals(booking.getBookingDate())) {

                String alreadyBookedTimeStart = alreadyBooked.getTimeStart();
                Date alreadyBookedTimeStartDate = simpleDateFormat.parse(alreadyBookedTimeStart);

                String alreaydBookedTimeEnd = alreadyBooked.getTimeEnd();
                Date alreaydBookedTimeEndDate = simpleDateFormat.parse(alreaydBookedTimeEnd);


                String bookingTimeStart = booking.getTimeStart();
                Date bookingTimeStartDate = simpleDateFormat.parse(bookingTimeStart);

                String bookingTimeEnd = booking.getTimeEnd();
                Date bookingTimeEndDate = simpleDateFormat.parse(bookingTimeEnd);

                if (!bookingTimeStartDate.before(alreadyBookedTimeStartDate) && !bookingTimeEndDate.after(alreaydBookedTimeEndDate)) {
                    return true;
                }
            }
        }
    }

I have an issue when the venue is already booked from 2:00 PM to 3:00 PM and we want to book it from 1:00 PM to 3:00 PM. This returns that the venue is not already booked which it is.

Is there a way to solve all the probabilities a between method in java?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalTime` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Dec 03 '19 at 03:58
  • Could you adapt the answers to this question to answer yours? [Determine Whether Two Date Ranges Overlap](https://stackoverflow.com/questions/325933/determine-whether-two-date-ranges-overlap) – Ole V.V. Dec 03 '19 at 03:59
  • Don’t keep start and end times as strings in your booking objects. Keep them as `LocalTime` objects. – Ole V.V. Dec 03 '19 at 04:04

1 Answers1

0

You need to check if there are overlaps, so the logic should be

if(!bookingTimeStartDate.before(alreadyBookedTimeStartDate) && 
!bookingTimeStartDate.after(alreaydBookedTimeEndDate) || 
!bookingTimeEndDate.before(alreaydBookedTimeStartDate) && 
!bookingTimeEndDate.after(alreaydBookedTimeEndDate))

This is the correct logic since if either of the times are not before the start and after the end, they must be in between and are therefore, overlapping.

Sam Black
  • 93
  • 1
  • 6
  • It does not really work in this case: 20/11/2019 | 9:00:00 AM | 8:00:00 AM and | 20/11/2019 11:00:00 AM | 10:00:00 AM | are already booked for the same venue. I can not book the same venue Date='20/11/2019', timeStart=' 9:00:00 AM', timeEnd=' 10:00:00 AM' – Marquise Mery Dec 02 '19 at 20:10
  • You'd have to add a check to see if they're equal if you also want to allow back to back bookings. – Sam Black Dec 02 '19 at 23:24
  • It’s overly complicated and it isn’t correct. – Ole V.V. Dec 03 '19 at 04:11