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

编程入门 行业动态 更新时间:2024-10-27 18:26:17
本文介绍了在java.time.LocalTime之间(第二天)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

请建议是否有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之间(第二天)

本文发布于:2023-11-24 02:52:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1623705.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:第二天   java   time   LocalTime

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!