Can a Timeframe be inserted in other timeframes without overlapping

时间:2018-02-03 09:04:27

标签: java

I'm having a TimeFrame class like this:

class TimeFrame {
    private LocalDateTime startTime;
    private LocalDateTime endTime;
}

Now I have a List of TimeFrames. I know want to check if I can insert a new TimeFrame in the list so that the times DO NOT overlap. Taken into account the dayStart and dayEnd time (constants during the whole app).

I'm having a hard time implementing this and I seems to me it can be done easier.

Can someone help me out and point me to the right direction.

1 个答案:

答案 0 :(得分:0)

Thanks to Boris The Spider.

Use Guava to solve the problem.

Step 1 create a Range for every timeframe like this:

 LocalDateTime startTime;
 LocalDateTime endTime;
 Range.closedOpen(startTime, endTime);

Step 2 create a RangeSet:

RangeSet rangeSet = TreeRangeSet.create(ranges);

Step 3 see if the target date intersects the ranges:

rangeSet.intersects(targetTimeFrame.timerange)