查找小时范围是否重叠,无论日期如何 [英] Find if hours ranges overlap regardless of the date

查看:145
本文介绍了查找小时范围是否重叠,无论日期如何的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一套两个小时的范围

I have a set of two hours ranges

10 PM - 02 AM
01 AM - 08 AM

我想检查他们中的任何一个是否过了一圈而不管日期。

I want to check if any of them over lap regardless of the the date.

例如:
第一个范围可能是8月1日和2日,而第二个范围可能是8月10日。

For Example: The first range could be on 1st and 2nd of Aug while the second range could be 10th of Aug.

这是我到目前为止

private Interval createInterval(final OpeningClosingTimesEntry entry) {
    LocalDateTime openingHour = LocalDateTime.fromDateFields(entry.getOpenTime());
    LocalDateTime closingHour = LocalDateTime.fromDateFields(entry.getCloseTime());
    if(closingHour.isBefore(openingHour)){
        closingHour = closingHour.plusDays(1);
    }
    return new Interval(openingHour.toDate().getTime(), closingHour.toDate().getTime());
}

private Interval adjustSecondIntervalDay(final Interval interval1,  final Interval interval2){
    if(interval1.getEnd().getDayOfYear() > interval2.getStart().getDayOfYear()){
        DateTime start = interval2.getStart().plusDays(1);
        DateTime end = interval2.getEnd().plusDays(1);
        return new Interval(start, end);
    }
    return interval2;
}


推荐答案

以下是如何正确完成使用Java 8的 LocalTime 。您询问Joda Time,但是您说您使用Java 8. Joda Time建议在大多数情况下切换到Java 8,这种情况也不例外。

Here's how to do it properly with Java 8's LocalTime. You ask about Joda Time, but you say you use Java 8. Joda Time recommends switching to Java 8 for most cases, and this case is no exception.

因为你不喜欢不关心日期,但只是想知道时间是否重叠,你不应该使用像 LocalDate LocalDateTime ,但是 LocalTime

Since you don't care about dates, but simply want to know if times overlap or not, you shouldn't use anything like LocalDate or LocalDateTime, but well LocalTime.

为了实现你的问题,我创建了 isBetween 方法,检查两个有序时间(-of-day)是否包含第三次,即使您传递到第二天。例如,21小时是18小时到6小时之间。

To implement your question, I created the isBetween method which check whether two ordered times(-of-day) contain a third time, even if you pass to the next day. So 21 h is between 18 h and 6 h, for instance.

然后,一旦你有了这种实用方法,你只需要检查两个中的至少一个范围包含另一个的边界。

Then, once you have that utility method, you simply have to check if at least one of the two ranges contains bounds of the other.

我告诉你关于边界的决定(例如1-2 - 2-3)。你有一般的算法,自己做出决定。

I leave up to you the decision about bounds themselves (1-2 -- 2-3, for instance). You have the general algorithm, make your own decision for the rest.

package so38810914;

import java.time.LocalTime;
import static java.util.Objects.*;

public class Question {

    public static class LocalTimeRange {

        private final LocalTime from;
        private final LocalTime to;

        public LocalTimeRange(LocalTime from, LocalTime to) {
            requireNonNull(from, "from must not be null");
            requireNonNull(to, "to must not be null");
            this.from = from;
            this.to = to;
        }

        public boolean overlaps(LocalTimeRange other) {
            requireNonNull(other, "other must not be null");
            return isBetween(other.from, this.from, this.to)
                    || isBetween(other.to, this.from, this.to)
                    || isBetween(this.from, other.from, other.to)
                    || isBetween(this.to, other.from, other.to);
        }

        private static boolean isBetween(LocalTime t, LocalTime from, LocalTime to) {
            if (from.isBefore(to)) { // same day
                return from.isBefore(t) && t.isBefore(to);
            } else { // spans to the next day.
                return from.isBefore(t) || t.isBefore(to);
            }
        }
    }

    public static void main(String[] args) {
        test( 0,  1,     2,  3,    false);
        test( 2,  3,     0,  1,    false);
        test( 0,  3,     1,  2,    true);
        test( 1,  2,     0,  3,    true);
        test( 0,  2,     1,  3,    true);
        test(12, 18,    15, 21,    true);
        test(18,  6,    21,  3,    true);
        test(21,  3,     0,  6,    true);
        test(21,  0,     3,  6,    false);

    }

    private static void test(int from1, int to1, int from2, int to2, boolean overlap) {
        LocalTimeRange range1 = new LocalTimeRange(LocalTime.of(from1, 0), LocalTime.of(to1, 0));
        LocalTimeRange range2 = new LocalTimeRange(LocalTime.of(from2, 0), LocalTime.of(to2, 0));
        boolean test = (range1.overlaps(range2)) == overlap;
        System.out.printf("[%2d-%2d] - [%2d-%2d] -> %-5b: %s%n", from1, to1, from2, to2, overlap, test?"OK":"Not OK");
    }
}

这篇关于查找小时范围是否重叠,无论日期如何的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆