在java.time.LocalTime之间(第二天)

请建议是否有API支持来确定我的时间是否在2个LocalTime实例之间,或建议采用不同的方法。

我有这个实体:

  class Place { LocalTime startDay; LocalTime endDay; } 

它存储工作日的开始和结束时间,即从’9:00’到’17:00’,或者从’22:00’到’5:00’的夜总会。

我需要实现一个Place.isOpen()方法,该方法确定该位置是否在给定时间打开。

一个简单的isBefore / isAfter在这里不起作用,因为我们还需要确定结束时间是否在第二天。

当然,我们可以比较开始和结束时间并做出决定,但我想要一些没有额外逻辑的东西,只需要一个简单的between()调用。 如果LocalTime不足以达到此目的,请另外建议。

如果我理解正确,你需要根据关闭时间是在开放时间(9-17)的同一天还是在第二天(22-5)进行两种情况。

它可能只是:

 public static boolean isOpen(LocalTime start, LocalTime end, LocalTime time) { if (start.isAfter(end)) { return !time.isBefore(start) || !time.isAfter(end); } else { return !time.isBefore(start) && !time.isAfter(end); } } 

这看起来更干净:

  if (start.isBefore(end)) { return start.isBefore(date.toLocalTime()) && end.isAfter(date.toLocalTime()); } else { return date.toLocalTime().isAfter(start) || date.toLocalTime().isBefore(end); }