java - java时间戳用间隔计算重叠持续时间
问题描述
我有许多时间戳 ( 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
然后创建新对象。
- 有点相似的是查找小时范围是否重叠,无论日期如何
编辑 2
toInterval
仅存在于 jodatime - 不是java.time
/ JSR-310。用 java.time 计算重叠持续时间的可行方法是什么?
编辑3
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
似乎有效,但仍然很笨拙。
解决方案
我相信以下方法为您提供了与您的 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 之前。
推荐阅读
- android - Place Picker 在 alpha 测试中崩溃,但在 android studio 中没有
- hibernate - 如何处理休眠死锁?(c3p0)
- python - Python金字塔练习题产生不同的输出
- javascript - 如何使用 Web Api 操作 AddSolutionComponent 将实体添加到带有 javascript 的解决方案中?
- mongodb - 如何在 graphql/prisma 中包含关系的类型创建查询?
- c# - 如何避免预选第一个列表项
- eclipse - 用于检查特定 Java 文件的 Eclipse 中未显示 Sonarlint 选项
- mysql - Sequelize 将 MySQL 自增 id 设置为 null
- visual-studio - 删除运行状况指示器和代码清理命令
- javascript - 如何对对象数组中的键进行排序并仅渲染一个对象