0

我正在做一个预订场地的项目。目的是如果场地在此期间已经预订,则不允许预订

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

当场地已在下午 2:00 至下午 3:00 预订时,我遇到了一个问题,我们想在下午 1:00 至下午 3:00 预订场地。这将返回该场地尚未被预订。

有没有办法解决java中方法之间的所有概率?

4

1 回答 1

0

你需要检查是否有重叠,所以逻辑应该是

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

这是正确的逻辑,因为如果任何一个时间都不是在开始之前和结束之后,它们必须在两者之间,因此是重叠的。

于 2019-12-02T15:07:57.567 回答