Home > Blockchain >  What is the best solution to get and compare hours minutes from LocalDateTime with given hour?
What is the best solution to get and compare hours minutes from LocalDateTime with given hour?

Time:03-05

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...

CodePudding user response:

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

CodePudding user response:

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