【问题标题】:How to check if time period is overlapping another time period irrespective of AM/PM无论上午/下午如何检查时间段是否与另一个时间段重叠
【发布时间】:2020-02-08 21:02:20
【问题描述】:

以下链接:How to check a timeperiod is overlapping another time period in java 有助于检查给定时间戳是否与另一个时间戳重叠。

但是,这仅适用于两个时间戳在 AMPM 中的情况。我要检查的是,给定的时间戳是否与另一个时间戳重叠,无论它是在 AM 还是 PM

例如: 如果我有一个Array 的时间戳(24 小时格式):

1:00 - 3:00

13:45 - 14:45

3:15 - 4:00.

前两个在技术上重叠,因为 13:45 - 14:45 介于 1:00-3:00 之间(如果转换为 12 小时格式)

如何检查相同的内容? 我想基本上检查时间戳之间是否有任何重叠(+/- 30 分钟)

【问题讨论】:

  • 所以您只想将时间视为模拟时钟上的时间?
  • 是的。刚刚意识到这是一个更好的类比。
  • 将每个下午时间转换为上午时间并使用该帖子中的一种方法有什么问题?
  • 你有内存限制吗?您可以将 12 小时格式的值与 24 小时通讯员复制并检查重叠的值吗?
  • 输入有什么限制吗?他们可以是 11:45 - 12:45 和 23:15 - 00:30 吗?如果我们知道每个时间间隔都在 00:00 - 12:00 或 12:00 - 24:00 之间,那就更简单了。另外,我是否正确理解最多 29 分钟的重叠应该算作没有重叠??

标签: java time


【解决方案1】:

假设每个区间都在 AM 或 PM 之内

我假设每个时间间隔完全在 AM(00:00 到 12)或 PM(12:00 到 00)内。我不明白您所说的“(+/- 30 分钟)”是什么意思,所以我忽略了它。

顺便说一句,我认为这是一个人为的挑战。在现实世界中,凌晨 2 点和下午 2 点并不相同,它们只是碰巧在 12 小时制上具有相同的表示。就像旗杆和波兰人一样,尽管他们都有“Pole”的表示,但它们并不相同。

正如 Sweeper 在评论中所建议的那样,我正在将每个间隔转换为 AM(如果它是在 PM),然后再进行映射。

    LocalTime begin1 = LocalTime.of(1, 0);
    LocalTime end1 = LocalTime.of(3, 0);
    LocalTime begin2 = LocalTime.of(13, 45);
    LocalTime end2 = LocalTime.of(14, 45);

    // Convert interval 1 to AM
    if (begin1.get(ChronoField.AMPM_OF_DAY) == 1) { // PM
        begin1 = begin1.minusHours(12);
        end1 = end1.minusHours(12);
    }
    // validate
    if (end1.isBefore(begin1)) {
        throw new IllegalStateException("end1 " + end1 + " must not be before begin1 " + begin1);
    }
    if (end1.isAfter(LocalTime.NOON)) {
        throw new IllegalStateException("Interval 1 must be completely within either AM or PM");
    }

    // Convert interval 2 to AM
    if (begin2.get(ChronoField.AMPM_OF_DAY) == 1) {
        begin2 = begin2.minusHours(12);
        end2 = end2.minusHours(12);
    }
    // validate
    if (end2.isBefore(begin2)) {
        throw new IllegalStateException("end2 " + end2 + " must not be before begin2 " + begin2);
    }
    if (end2.isAfter(LocalTime.NOON)) {
        throw new IllegalStateException("Interval 2 must be completely within either AM or PM");
    }

    if (end2.isAfter(begin1) && end1.isAfter(begin2)) {
        System.out.println("They overlap");
    } else {
        System.out.println("They do not overlap");
    }

这段代码的输出是:

它们重叠

极端情况:我接受上午间隔的结束时间为 12:00(中午),下午间隔的结束时间为 00:00。 LocalTime.minusHours() 有循环下溢,所以从 00:00 减去 12 小时得到 12:00。

如果您定义一个带有 begin 和 end 字段的 TimePeriod 类、检查重叠的方法和转换为 AM 的辅助方法,则代码可能会更简单、更容易找到。

没有限制

编辑:假设每个间隔可以是从 0(含)到 24 小时(不含)的任意长度,并且可能跨越 00:00,这有点复杂,但我不能让挑战休息。

一些观察:

  1. 如果一个间隔为 12 小时或更长,而另一个的长度非零,则两者必然重叠。
  2. 如果两个间隔都短于 12 小时,那么如果它们不重叠,我们可以按此处给出的顺序从begin1end1begin2 循环前进(计数)到end2 和 要么 不超过 12 点钟 超过 12 点一次并在begin1 之前结束。如果这个循环不起作用,那么间隔必须以某种方式重叠。

在代码中:

public static boolean overlaps(LocalTime begin1, LocalTime end1, LocalTime begin2, LocalTime end2) {
    if (begin1.equals(end1)) { // zero length, cannot overlap anything
        return false;
    }
    if (begin2.equals(end2)) {
        return false;
    }

    // If any interval is 12 hours or longer,
    // the other one is necessarily included, that is, overlaps
    if (is12HoursOrLonger(begin1, end1)) {
        return true;
    }
    if (is12HoursOrLonger(begin2, end2)) {
        return true;
    }

    // Convert all times to AM
    begin1 = toAm(begin1);
    end1 = toAm(end1);
    begin2 = toAm(begin2);
    end2 = toAm(end2);

    // For the two intervals *not* to overlap we must be able to go forward
    // from begin1 through end1 and begin2 to end2 in this order either
    // not crossing 12 or crossing 12 once and ending before or on begin1
    boolean crossed12OClock = false;
    if (end1.isBefore(begin1)) { // to go forward to end1 we are crossing 12 o’clock
        crossed12OClock = true;
    }
    if (begin2.isBefore(end1)) {
        if (crossed12OClock) {
            // crossing 12 for the second time;
            // intervals cannot be in non-overlapping order
            return true;
        }
        crossed12OClock = true;
    }
    if (end2.isBefore(begin2)) {
        if (crossed12OClock) {
            return true;
        }
        crossed12OClock = true;
    }
    if (crossed12OClock) {
        return end2.isAfter(begin1);
    } else {
        return false;
    }
}

此方法使用以下两种辅助方法:

private static boolean is12HoursOrLonger(LocalTime begin, LocalTime end) {
    Duration length = Duration.between(begin, end);
    if (length.isNegative()) {
        length = length.plusDays(1);
    }
    return ! length.minusHours(12).isNegative();
}

private static LocalTime toAm(LocalTime time) {
    return time.with(ChronoField.AMPM_OF_DAY, 0);
}

让我们用以前的时间来试试吧:

    if (overlaps(begin1, end1, begin2, end2)) {
        System.out.println("They overlap");
    } else {
        System.out.println("They do not overlap");
    }

它们重叠

由于代码和参数很复杂,请务必使用单元测试彻底覆盖方法。

【讨论】:

    猜你喜欢
    • 2013-06-10
    • 1970-01-01
    • 2021-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-28
    • 1970-01-01
    相关资源
    最近更新 更多