在java.time.LocalTime之间(第二天) [英] between java.time.LocalTime (next day)
问题描述
请建议是否有API支持以确定我的时间是否介于2 LocalTime
实例之间,或建议采用其他方法。
Please suggest if there is an API support to determine if my time is between 2 LocalTime
instances, or suggest a different approach.
我有这个实体:
class Place {
LocalTime startDay;
LocalTime endDay;
}
存储工作日的开始和结束时间,即从'9:00开始'直到'17'',或者从'22:00'到'5:00'的夜总会。
which stores the working day start and end time, i.e. from '9:00' till '17:00', or a nightclub from '22:00' till "5:00".
我需要实现一个 Place.isOpen()
确定该地点是否在给定时间开放的方法。
I need to implement a Place.isOpen()
method that determines if the place is open at a given time.
一个简单的 isBefore
/ isAfter
在这里不起作用,因为我们还需要确定结束时间是否在第二天。
A simple isBefore
/isAfter
does not work here, because we also need to determine if the end time is on the next day.
当然,我们可以比较开始和结束时间并做出决定,但我想要一些没有额外逻辑的东西,只需要一个简单的()
呼叫。如果 LocalTime
不足以达到此目的,请另外建议。
Of course, we can compare the start and end times and make a decision, but I want something without additional logic, just a simple between()
call. If LocalTime
is not sufficient for this purpose, please suggest other.
推荐答案
如果我理解正确,你需要根据截止时间是在开放时间(9-17)或第二天(22-5)的同一天制作两种情况。
If I understand correctly, you need to make two cases depending on whether the closing time is on the same day as the opening time (9-17) or on the next day (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);
}
}
这篇关于在java.time.LocalTime之间(第二天)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!