获取和比较时间的最佳解决方案是什么?给定时间的 LocalDateTime 分钟数?
我正在实现一项服务(不会随时随地投入生产),该服务应该接收 LocalDateTime
和 Duration
,并且应该检查给定时间是否在公司工作时间之间(即 8 :00-22:00),工作时间应该(以某种方式)可配置:
假设我有一个:
public class CompanyWorkingHoursService {
private static final Int OPENING_HOUR = 8;
private static final Int CLOSING_HOUR = 22;
private boolean isMeetingBetweenWorkingHours(LocalDateTime beginningDateTime, Duration duration) {
LocalDateTime endingDateTime = beginningDateTime.plus(duration);
}
并且我被困住了。
我可以将 OPENING_HOUR
和 CLOSING_HOUR
的类型更改为我想要的任何类型。我可以从 LocalDateTime
获取小时和分钟,但这些都是整数。我不想比较整个日期 - 我只需要小时和分钟。
我找到了一些使用 java.util.Date
的解决方案,但如果可能的话,我想继续使用 LocalDateTime
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“最好”的事情是避免使用整数。因此,将开始和结束时间定义为
LocalTime
,并使用isAfter()
、isBefore()
和equals( )
由LocalTime
提供:The "best" thing is to avoid integers. So define the opening and closing hours as
LocalTime
, and compare the dates using theisAfter()
,isBefore()
andequals()
provided byLocalTime
:如果工作时间应该(以某种方式)可配置,您也可以将它们传递给该方法。然后根据这些值结合会议日期创建
LocalDateTime
实例。也许是这样的:
If the working hours should be (somehow) configurable, you could pass them to the method, too. Afterwards create
LocalDateTime
instances from those values in combination with the date of the meeting.Maybe like this: