获取两个时间段的交集区间

public static float getTimeInterval(Date stime1, Date etime1, Date stime2, Date etime2) throws Exception {
        float f = 0;
        long lst = stime1.getTime();
        long let = etime1.getTime();

        long rst = stime2.getTime();
        long ret = etime2.getTime();

        if (lst > let || rst > ret) {
            throw new Exception("起始时间不能大于结束时间");
        }

        if (let <= rst || lst >= ret) {
            return f;
        }

        long[] a = {lst, let, rst, ret};
        Arrays.sort(a); //从小到大排序,取第二、第三计算
        f = a[2] - a[1];

        log.info("f=" + f);

        return (float) Math.round((f / 3600000) * 100) / 100;
    }

你可能感兴趣的:(JAVA)