如何检查两个时间范围是否重叠?

时间:2016-04-29 04:56:34

标签: java

我有一个要求,我每天可以创建3班。轮班时间是:

 06:00 - 14:00   
 14:01 - 22:00   
 22:01 - 05:59. 

如何检查换档时间是否重叠?请注意,在第三种情况下,从小时大于到小时

1 个答案:

答案 0 :(得分:1)

最后我解决了自己的问题。如下请见。

public void checkIfTimesOverlapped(){


// eft - existed FROM TIME
// ett - existed TO TIME
// cft - current entered FROM TIME
// ctt - current entered TO TIME
// Note : same FROM TIME and TO TIME is not allowed. so not checking. 

float eft = 22.00f; 
float ett = 05.00f;
float cft = 23.00f;
float ctt = 24.00f;


    if (cft < ctt) {
        if (eft < ett) {
            if(cft<=ett && ctt>=eft){
                System.out.println("overlapped");
            }

        } else if(eft > ett) {
            if(!(cft>ett && ctt<eft)){
                System.out.println("overlapped");
            }
        }
    } else if (cft > ctt) {

        if (eft < ett) {
            if((cft>=eft && cft<=ett) || (ctt>=eft && ctt<=ett)){
                System.out.println("overlapped");
            }

        } else if(eft > ett) {
            // already one shift existed with FROM TIME > TO TIME. so again FROM TIME > TO TIME is not possible
            System.out.println("overlapped");
        }

    }

}