between java.time.LocalTime (next day)

蹲街弑〆低调 提交于 2019-11-30 18:54:58

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

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

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);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!