Java时间戳以间隔

时间:2018-07-22 14:08:12

标签: java time intervals overlap duration

我有很多时间戳(start, end),它们定义了一个间隔,并且想要有效地检查它们是否与另一个单个间隔重叠。如果是,则计算重叠持续时间,否则返回0。

间隔:18:00直到第二天08:00

start                  | end
2018-01-02 14:59:18.922|2018-01-02 14:59:38.804
2018-01-02 18:32:59.348|2018-01-02 20:30:41.192
2018-01-02 01:54:59.363|2018-01-02 01:54:59.363
2018-01-03 00:10:38.831|2018-01-03 00:11:53.103

我不确定如何有效地定义第二天

编辑

LocalDate

具有方法toInterval().overlaps(anotherInterval)。我只是不确定如何以通用的方式获取拟合间隔(第二天18:00-08:00),即无需手动读取YYYMMDD然后创建新对象。< / p>

编辑2

toInterval仅在jodatime出现-java.time / JSR-310不存在。用java.time计算重叠持续时间的可行方法是什么?

https://stackoverflow.com/questions/325933/determine-whether-two-date-ranges-overlap

edit3

使用jodaTime的解决方案:

val begin = new DateTime(new java.sql.Timestamp().getTime())
val stop = new DateTime(new java.sql.Timestamp().getTime())
val i1 = new Interval(begin, stop)

val start = new DateTime(begin.year.get   , begin.monthOfYear.get, begin.dayOfMonth.get, startHour, 0, 0, 0);
val endIntermediate =stop.toDateTime.plusDays(1)
val end = new DateTime(endIntermediate.year.get   , endIntermediate.monthOfYear.get, endIntermediate.dayOfMonth.get, endHour, 0, 0, 0);
val i2 = new Interval(start, end)

val overlap = i1.overlap(i2)
val overlapDurationOrNull = overlap.toDuration

似乎可以工作,但仍然很笨拙。

2 个答案:

答案 0 :(得分:3)

我相信以下方法可以为您提供与Joda-Time解决方案相当的功能。

private static final LocalTime START = LocalTime.of(18, 0);
private static final LocalTime END = LocalTime.of(8, 0);

public static Duration overlap(ZonedDateTime currentStart, ZonedDateTime currentEnd) {
    ZonedDateTime singleIntervalStart = currentStart.with(START);
    ZonedDateTime singleIntervalEnd = currentStart.plusDays(1).with(END);
    if (currentEnd.isBefore(singleIntervalStart)) {
        // no overlap
        return Duration.ZERO;
    }
    ZonedDateTime overlapStart = currentStart.isBefore(singleIntervalStart)
            ? singleIntervalStart : currentStart;
    ZonedDateTime overlapEnd = currentEnd.isBefore(singleIntervalEnd)
            ? currentEnd : singleIntervalEnd;
    return Duration.between(overlapStart, overlapEnd);
}

为了使用您问题中的时间戳进行尝试,我使用以下实用程序方法:

private static void demo(String from, String to) {
    ZoneId zone = ZoneId.of("Atlantic/Stanley");
    Duration overlapDuration = overlap(LocalDateTime.parse(from).atZone(zone),
            LocalDateTime.parse(to).atZone(zone));
    System.out.println("" + from + " - " + to + ": " + overlapDuration);
}

现在我这样称呼它:

    demo("2018-01-02T14:59:18.922", "2018-01-02T14:59:38.804");
    demo("2018-01-02T18:32:59.348", "2018-01-02T20:30:41.192");
    demo("2018-01-02T01:54:59.363", "2018-01-02T01:54:59.363");
    demo("2018-01-03T00:10:38.831", "2018-01-03T00:11:53.103");

输出为:

2018-01-02T14:59:18.922 - 2018-01-02T14:59:38.804: PT0S
2018-01-02T18:32:59.348 - 2018-01-02T20:30:41.192: PT1H57M41.844S
2018-01-02T01:54:59.363 - 2018-01-02T01:54:59.363: PT0S
2018-01-03T00:10:38.831 - 2018-01-03T00:11:53.103: PT0S

在第一个示例中14:59在18:00之前,因此结果为0重叠。在第二个示例中,整个时间间隔计为重叠(将近2小时)。请注意,在最后两个示例中,没有报告重叠,因为时间是18:00之前的许多小时。我不确定这是否是您想要的,因为时间也是在08:00之前。

答案 1 :(得分:1)

您可以简单地使用LocalDate.plusDays添加一天。

假设要进行以下比较的迭代:

LocalDateTime d1 = LocalDateTime.parse("2018-01-02T14:59:18"),
              d2 = LocalDateTime.parse("2018-01-02T14:59:38");

您可以使用以下方法创建18:0008:00日期/时间对象:

LocalDateTime start = LocalDateTime.of(d1.toLocalDate(), LocalTime.of(18, 0));
LocalDateTime end = LocalDateTime.of(d1.toLocalDate().plusDays(1), 
                                       LocalTime.of(8, 0));

我假设18:00d1在同一天。