相同的if语句中的Date.after和Data.after

时间:2018-12-06 01:55:28

标签: java date intervals date-comparison java-date

如果我有一个房间列表和一个名为room1的特定房间,该房间在2018-12-12和2018-12-20之间预订。

另一个用户想要在2018-12-15和2018-12-25之间预订该房间。我尝试使用date.after(checkInDate)date.after(checkOutDate),但是没有用。怎么解决?

 Date tempStart = checkinDate;
 Date tempEnd = checkoutDate;

 LinkedList<Integer> roomNbrs = new LinkedList<>();

 for (Booking b: books) {

     if (tempStart.equals(b.getCheckinDate()) && tempEnd.equals(b.getCheckoutDate()) && !roomNbrs.contains(roomNbr) ||
                (tempStart.after(b.getCheckinDate())) && ((tempEnd.before(b.getCheckoutDate()) || tempEnd.equals(b.getCheckoutDate()))
                        && !roomNbrs.contains(roomNbr)) ||
                ((tempStart.before(b.getCheckinDate()) || tempStart.equals(b.getCheckinDate()))
                        && tempEnd.before(b.getCheckoutDate()) && !roomNbrs.contains(roomNbr))){

             roomNbrs.add(b.getRoomNbr());
     }
}

5 个答案:

答案 0 :(得分:2)

我设法解决了问题,这是解决方案:

private void viewAvailableRoomDate(Date tempStart, Date tempEnd) {

    LinkedList<Integer> roomNbrs = new LinkedList<>();

    for (Booking b : books) {

        if ((!(tempStart.after(b.getCheckinDate()) && tempEnd.after(b.getCheckinDate())) ||
                (tempStart.before(b.getCheckoutDate()) && tempEnd.after(b.getCheckoutDate())) ||
                (tempStart.before(b.getCheckinDate()) && tempEnd.after(b.getCheckoutDate()))) ||
                (tempStart.after(b.getCheckinDate()) && tempEnd.before(b.getCheckoutDate()))){
            roomNbrs.add(b.getRoomNbr());
        }
    }

}

答案 1 :(得分:1)

scheduling checkoutDate应该before room1.checkinDatescheduling checkinDate after room1.checkoutDate


重述: 新会议的结束日期应该在room1的开始日期之前,或者新会议的开始日期应该在room1的结束日期之后。

答案 2 :(得分:1)

据我所知,您的整体逻辑存在缺陷。您无法查看单个预订并确定该预订的房间是否在需要的时间段内(在2018-12-15至2018-12-25之间)可用。

想象:

  • 第1室预订于2018-12-12和2018-12-20之间。
  • 房间2在2018-12-20和2018-12-30之间预订,并在2019-01-10和2019-01-17之间再次预订。
  • 3号房根本没有预订。

现在1号和2号会议室在所需时间内均不可用,但3号会议室可用。

当您遍历预订时:

  • 您查看的是1号房的预订,它与想要的房间重叠,因此您不会在列表中加1。
  • 您看到2号房的第一笔预订,它重叠了,您什么也不做。
  • 现在,您查看房间2的第二笔预订。它不重叠,因此您将房间2添加到房间号列表中。

没有更多的预订,因此我们完成了。现在,您的列表错误地包含了2号房间。相反,它应该包含3号房间,但是没有。

因此,除了您的预订之外,您还需要一个房间列表。

BTW检查特定预订是否与所需时间重叠很简单。我给你伪代码:

    if (tempEnd is before booking start) {
        // no overlap
    } else if (tempStart is after booking end) {
        // also no overlap
    } else {
        // overlap
    }

这也是exudong’s answer已经说过的话。

最后:您使用的Date类存在设计问题,并且已经过时。它(尽管其名称)也不适合表示日期,它是一个时间点。相反,我热烈建议您使用现代Java日期和时间API java.time中的LocalDate

链接:Oracle tutorial: Date Time解释了如何使用java.time

答案 3 :(得分:0)

我不能在相同的if语句中执行此操作,但是如果您可以妥协以拥有两个...:

for (Booking b : books) {
    if (roomNbrs.contains(b.getRoomNbr()) {

        continue;
    }
    if (tempStart.after(b.getCheckoutDate()) && tempEnd.before(b.getCheckinDate()) {
        roomNbrs.add(b.getRoomNbr());
    }
}

答案 4 :(得分:0)

在if语句中,您具有树条件:

  1. 两个亲戚日期和两个结帐日期都相等
  2. tempStartb.getCheckinDate()之后,而tempEndb.getCheckoutDate()之前或等于
  3. tempStartb.getCheckinDate()之前或等于tempEndb.getCheckoutDate()之前

这3个都不包含tempStartb.getCheckinDate()之前而tempEnd在b.getCheckoutDate()之后的日期

如果您要查看房间在此期间是否可用,我认为更容易测试临时日期是在预订之前还是之后。然后您将具有以下两个条件:

  1. tempStartb.getCheckinDate()之前,而tempEndb.getCheckinDate()之前或等于
  2. tempStartb.getCheckoutDate()之后或等于

条件代码将如下所示:

tempIsBeforeBooking = tempStart.before(b.getCheckinDate()) 
                          && (tempEnd.before(b.getCheckoutDate()) 
                              || tempEnd.equals(b.getCheckinDate());


tempIsAfterBooking = tempStart.after(b.getCheckoutDate()) 
                         || tempStart.equals(b.getCheckoutDate());