我正在研究一个用于预订场地的项目。目的是如果场地在此期间已经在预订,则不允许预订
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 PM到3:00 PM预定并且我们想要从1:00 PM到3:00 PM预定时,我遇到一个问题。这将返回尚未预订的场所。
有没有一种方法可以解决Java中的between方法之间的所有可能性?
答案 0 :(得分:0)
您需要检查是否存在重叠,因此逻辑应该是
if(!bookingTimeStartDate.before(alreadyBookedTimeStartDate) &&
!bookingTimeStartDate.after(alreaydBookedTimeEndDate) ||
!bookingTimeEndDate.before(alreaydBookedTimeStartDate) &&
!bookingTimeEndDate.after(alreaydBookedTimeEndDate))
这是正确的逻辑,因为如果两个时间中的任何一个都不在开始之前和结束之后,则它们必须介于两者之间,因此是重叠的。