获取和比较时间的最佳解决方案是什么?给定时间的 LocalDateTime 分钟数?

发布于 2025-01-11 19:37:22 字数 766 浏览 1 评论 0 原文

我正在实现一项服务(不会随时随地投入生产),该服务应该接收 LocalDateTimeDuration ,并且应该检查给定时间是否在公司工作时间之间(即 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_HOURCLOSING_HOUR 的类型更改为我想要的任何类型。我可以从 LocalDateTime 获取小时和分钟,但这些都是整数。我不想比较整个日期 - 我只需要小时和分钟。

我找到了一些使用 java.util.Date 的解决方案,但如果可能的话,我想继续使用 LocalDateTime...

I am implementing a service (not going to production anywhere anytime) which should receive a LocalDateTime and a Duration and should check if given time is between company working hours (which are 8:00-22:00), the working hours should be (somehow) configurable:

lets say that I have a:

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);
    }

and I'm stuck.

I can change the type of OPENING_HOUR and CLOSING_HOUR to whatever I want. I can get hours and minutes from LocalDateTime but those are integers. And I don't want to compare whole dates - i need just hours and minutes.

I have found some solutions using java.util.Date but I would like to stay with LocalDateTime if possible...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抽个烟儿 2025-01-18 19:37:23

“最好”的事情是避免使用整数。因此,将开始和结束时间定义为 LocalTime,并使用 isAfter()isBefore()equals( )LocalTime提供:

private static final LocalTime OPENING_HOUR = LocalTime.of(8, 0);
private static final LocalTime CLOSING_HOUR = LocalTime.of(22, 0);

private boolean isMeetingBetweenWorkingHours(LocalDateTime beginningDateTime, Duration duration) {
    LocalDateTime endingDateTime = beginningDateTime.plus(duration);
    return !beginningDateTime.toLocalTime().isBefore(OPENING_HOUR)
            && !endingDateTime.toLocalTime().isAfter(CLOSING_HOUR));
}

The "best" thing is to avoid integers. So define the opening and closing hours as LocalTime, and compare the dates using the isAfter(), isBefore() and equals() provided by LocalTime:

private static final LocalTime OPENING_HOUR = LocalTime.of(8, 0);
private static final LocalTime CLOSING_HOUR = LocalTime.of(22, 0);

private boolean isMeetingBetweenWorkingHours(LocalDateTime beginningDateTime, Duration duration) {
    LocalDateTime endingDateTime = beginningDateTime.plus(duration);
    return !beginningDateTime.toLocalTime().isBefore(OPENING_HOUR)
            && !endingDateTime.toLocalTime().isAfter(CLOSING_HOUR));
}
如果没有你 2025-01-18 19:37:23

如果工作时间应该(以某种方式)可配置,您也可以将它们传递给该方法。然后根据这些值结合会议日期创建 LocalDateTime 实例。

也许是这样的:

public static boolean isMeetingBetweenWorkingHours(
                LocalDateTime startMeeting, Duration meetingDuration,
                int openFrom, int openUntil) { // pass start and end hour of day
    /* 
     * create the working time hours using the hours of day passed 
     * and using the date of the meeting start passed
     */
    LocalDateTime startWorkingHours = LocalDateTime.of(startMeeting.toLocalDate(),
                                                        LocalTime.of(openFrom, 0));
    LocalDateTime endWorkingHours = LocalDateTime.of(startMeeting.toLocalDate(),
                                                        LocalTime.of(openUntil, 0));
    // calculate the end time of the meeting
    LocalDateTime endMeeting = startMeeting.plus(meetingDuration);
    // then return if the meeting fully fits into the working time slot
    return !startMeeting.isBefore(startWorkingHours)
            && !endMeeting.isAfter(endWorkingHours);
}

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:

public static boolean isMeetingBetweenWorkingHours(
                LocalDateTime startMeeting, Duration meetingDuration,
                int openFrom, int openUntil) { // pass start and end hour of day
    /* 
     * create the working time hours using the hours of day passed 
     * and using the date of the meeting start passed
     */
    LocalDateTime startWorkingHours = LocalDateTime.of(startMeeting.toLocalDate(),
                                                        LocalTime.of(openFrom, 0));
    LocalDateTime endWorkingHours = LocalDateTime.of(startMeeting.toLocalDate(),
                                                        LocalTime.of(openUntil, 0));
    // calculate the end time of the meeting
    LocalDateTime endMeeting = startMeeting.plus(meetingDuration);
    // then return if the meeting fully fits into the working time slot
    return !startMeeting.isBefore(startWorkingHours)
            && !endMeeting.isAfter(endWorkingHours);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文