between java.time.LocalTime (next day)
Asked Answered
B

3

12

Please suggest if there is an API support to determine if my time is between 2 LocalTime instances, or suggest a different approach.

I have this entity:

 class Place {
   LocalTime startDay;
   LocalTime endDay;
 }

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

I need to implement a Place.isOpen() method that determines if the place is open at a given time.

A simple isBefore/isAfter does not work here, because we also need to determine if the end time is on the next day.

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.

Benzene answered 4/2, 2016 at 12:25 Comment(2)
Post something that you have tried. How to you think you will handle the case where the closing time is "before" (without considering the date) the opening time?Glycerite
"Of course, we can compare two times, and make a decision, but I don't think this is nice." Why do you think that? What solution are you looking for? Define "nice way".Glycerite
U
17

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

It could simply be:

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);
  }
}
Unrig answered 4/2, 2016 at 14:8 Comment(2)
Thank you, this is what I ended up with. However this seems like a popular usecase, and I was wondering if there is any support from the API. Or maybe some structure that would store that extra day, to make just one comparison at a time. But if figured out, that this will not work, since you need to determine this 'next day' thing on the input time as well. Life is pain :) I'll mark your answer as solution.Benzene
I don't think there is a simpler way.Unrig
Y
0

This looks cleaner for me:

 if (start.isBefore(end)) {
     return start.isBefore(date.toLocalTime()) && end.isAfter(date.toLocalTime());
 } else {
     return date.toLocalTime().isAfter(start) || date.toLocalTime().isBefore(end);
 }
Yount answered 18/8, 2017 at 9:42 Comment(0)
P
0

I have refactored @assylias answer so i use int instead of local time as i get open and close hour from api int integer format

public static boolean isOpen(int start, int end, int time) {
    if (start>end) {
        return time>(start) || time<(end);
    } else {
        return time>(start) && time<(end);
    }
}
public static boolean isOpen(int start, int end) {
    SimpleDateFormat sdf = new SimpleDateFormat("HH");
    Date resultdate = new Date();
    String hour = sdf.format(resultdate);
    int time = Integer.valueOf(hour);
    if (start>end) {
        return time>(start) || time<(end);
    } else {
        return time>(start) && time<(end);
    }
}
Propagation answered 7/8, 2019 at 11:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.