2

问题:我有一个包含小时数的列表,例如:08:15:00 08:45:00 09:00:00 12:00:00 ...应用程序允许用户预约特定时间: 8:15:00,每次开会半小时。

问题:如何确定这样的预约是否需要空档?我知道 Calendar 类有 before() 和 after() 方法,但它不能解决我的问题。我的意思是如果12:00有预约,12:00有预约,那么在12:15再预约之前如何预防?

编辑:

我尝试过使用我之前提到的方法,例如:

Calendar cal1 = Calendar.getInstance(); // for example 12:00:00
Calendar cal2 = Calendar.getInstance(); // for exmaple 12:30:00
Calendar userTime = Calendar.getInstance(); // time to test: 12:15:00

if(user.after(cal1)&& user.before(cal2)){
... // do sth
}
4

3 回答 3

5

检查要检查的日期是否在提供的两者之间:

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm");
Date before = sdf.parse("07/05/2012 08:00");
Date after = sdf.parse("07/05/2012 08:30");
Date toCheck = sdf.parse("07/05/2012 08:15");
//is toCheck between the two?
boolean isAvailable = (before.getTime() < toCheck.getTime()) && after.getTime() > toCheck.getTime();

要预订一个确定的时间,我会做一个有两个日期的课程和一个检查这个的方法:

public class Appointment{

 private Date start;
 private Date end;

 public boolean isBetween(Date toCheck){....}

}

然后你可以简单地做一个Schedule类扩展ArrayList,添加一个方法isDateAvailable(Date toCheck),迭代约会列表并检查没有人冲突。

于 2012-05-07T20:52:46.343 回答
1

我会有某种约会类,它有一个开始时间戳和一个持续时间,或者一个开始时间和一个结束时间。然后,在向日程表中添加新约会时,检查开始时间在新约会之前的约会不会超过建议的新约会的开始时间。

于 2012-05-07T20:47:35.977 回答
1

好吧,您将如何做具体取决于您如何存储数据、格式等,但通常您要做的只是检查在请求时间到请求时间 + 请求长度之间的任何时间是否有约会。

// Example (using int time(1 = 1 minute), assuming that appointments can only be at 15min intervals)
boolean isHalfHourTimeSlotAvaliable(int time) {
    for (int i = 0; i < appointments.size(); i++) {
        if (appointments.get(i).time == time || appointments.get(i).time == time + 15) {
            return false;
        }
    }
    return true;
}
于 2012-05-07T20:47:56.130 回答