2

The opening and closing hours for the company are 8am to 10 pm. The code below seems to add 4 hours on to whatever time is entered into the text field. So if I change the opening time to 11:59 I get the correct 8 am start date, but it's not possible to change the end time to 26:01 so I can't get the end time to work correctly. The data has to be stored in the database in UTC, but for display purposes it's displayed in EST.

public static boolean insideBusinessHours(String startTime, String endTime, String date) {

   
    LocalDateTime localStart = stringToLDT_UTC(startTime, date);
    LocalDateTime localEnd = stringToLDT_UTC(endTime, date);
    String UTCstart = localStart.toString().substring(11,16);
    String UTCend = localEnd.toString().substring(11,16);
    
    LocalTime enteredStart = LocalTime.parse(UTCstart);
    LocalTime enteredEnd = LocalTime.parse(UTCend);
    LocalTime openingHour = LocalTime.of(07, 59);
    LocalTime closingHour = LocalTime.of(22, 1);
    Boolean startTimeAllowed = enteredStart.isAfter(openingHour);
    Boolean endTimeAllowed = enteredEnd.isBefore(closingHour);

    if (startTimeAllowed && endTimeAllowed) {
        return true;
    }
    else {
        return false;
    }

}

public static LocalDateTime stringToLDT_UTC(String time, String date) {
    DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    LocalDateTime ldt = LocalDateTime.parse(date + " " + time, format)
            .atZone(ZoneId.systemDefault())
            .withZoneSameInstant(ZoneId.of("UTC"))
            .toLocalDateTime();
    return ldt;
}
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Gleaty
  • 21
  • 1
  • I added the java and java-time tags. Always add at least one tag for the programming language you are using (if one). Your question is fine and answerable, and I suspect that the lack of response until now stems from Java programmers not discovering that your question was there. – Ole V.V. Sep 04 '21 at 12:22
  • *"not discovering that your question was there"* — True story. – MC Emperor Sep 04 '21 at 17:43

1 Answers1

3

Even though dates and times need to be stored in UTC, I believe that it makes more sense to do the comparisons in your local time.

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

public static boolean insideBusinessHours(String startTime, String endTime) {
    LocalTime start = LocalTime.parse(startTime);
    LocalTime end = LocalTime.parse(endTime);
    boolean startTimeAllowed = !start.isBefore(OPENING_HOUR);
    boolean endTimeAllowed = !end.isAfter(CLOSING_HOUR);
    return startTimeAllowed && endTimeAllowed;
}

Further comments:

  • You don’t need the date if the opening hours are valid every day (you would need it to convert to or from UTC, which I have avoided).
  • It seems you had added a minute in each end to make sure to allow start and end to fall on the opening and closing hour, respectively. It’s more correct to require the start time to fall on or after the opening time. Similarly the end time before or on the closing time. In the code I am using not before to mean on or after.
  • In Java integer literals with a leading 0 are interpreted as octal. So don’t use a leading 0 unless you intended octal. You may find that 07 for the hour of the day looks nice. but you will get a surprise when 08 does not work. And in a different context, your users could get a pretty unpleasant surprise when for example 0700 does work but denotes 448.
  • Don’t use Boolean objects. Don’t ever if you can avoid it since the risk of a null reference to a Boolean is confusing and from experience often leads to errors. Use primitive boolean (lower case b).
  • Most seasoned programmers prefer return startTimeAllowed && endTimeAllowed; as terser than your if-else statement.

Link

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161