下面是在自定義的TimeInterval所嘗試(很像解決灰色評論):
import org.joda.time.*;
public class TimeInterval {
private static final Instant CONSTANT = new Instant(0);
private final LocalTime from;
private final LocalTime to;
public TimeInterval(LocalTime from, LocalTime to) {
this.from = from;
this.to = to;
}
public boolean isValid() {
try { return toInterval() != null; }
catch (IllegalArgumentException e) { return false;}
}
public boolean overlapsWith(TimeInterval timeInterval) {
return this.toInterval().overlaps(timeInterval.toInterval());
}
/**
* @return this represented as a proper Interval
* @throws IllegalArgumentException if invalid (to is before from)
*/
private Interval toInterval() throws IllegalArgumentException {
return new Interval(from.toDateTime(CONSTANT), to.toDateTime(CONSTANT));
}
}
我想你已經提供了最好的答案傢伙。我將它作爲一個具有開始和結束的LocalTime的OpenTimeRange實現,然後使用它們的數組來指定一天的開放時間。 – Gray 2012-01-11 22:54:04