3
\$\begingroup\$

Novice Java developer here. I've never really used a time/date library before and I'm curious how an experienced developer would solve this. You're given 4 ints: startHour, startMinute, endHour and endMinute. Now, check if current time is within the given timeframe. Is there a more clean way of doing this than what I've done here:

private void checkTimeframe(int startHour, int startMinute, int endHour, int endMinute) {
    LocalDateTime now = LocalDateTime.now();
    LocalTime localTimeStart = new LocalTime(startHour, startMinute);
    LocalTime localTimeEnd = new LocalTime(endHour, endMinute);

    LocalDateTime startTime = new LocalDateTime(now.getYear(), now.getMonthOfYear(),
            now.getDayOfMonth(), startHour, startMinute);

    LocalDateTime endTime = new LocalDateTime(now.getYear(), now.getMonthOfYear(),
            now.getDayOfMonth(), endHour, endMinute);

    //Check if start/end is, for instance, 23:00 - 03:00
    if (localTimeStart.isAfter(localTimeEnd) || localTimeStart.equals(localTimeEnd)) {
        endTime = endTime.plusDays(1);
    }

    if ( (now.equals(startTime) || now.isAfter(startTime) ) && now.isBefore(endTime)) {
        System.out.println("Ok, we're within start/end");
    } else {
        System.out.println("Outside start/end");
    }
}
\$\endgroup\$
0

2 Answers 2

1
\$\begingroup\$

to me it looks like a simple check of number between range of numbers. do I would do like this

  1. turn hour and minute into one number that is hhmm to simplify the comparison

  2. now its a simple check between range of numbers, taking into account the case of cross-date boundary

complete code:

private static void checkTimeframe(int startHour, int startMinute, int endHour, int endMinute) {
    // "concatanate" hour and minute into one number
    int startHourMinute = startHour * 100 + startMinute;
    int endHourMinute = endHour * 100 + endMinute;
    LocalDateTime now = LocalDateTime.now();
    int nowHourMinute = now.getHour() * 100 + now.getMinute();

    // if range within date - simple between boundaries check
    if (startHourMinute <= endHourMinute) {
        if (nowHourMinute >= startHourMinute && nowHourMinute <= endHourMinute) {
            System.out.println("Ok, we're within start/end");
        } else {
            System.out.println("Outside start/end");
        }
    // else (cross date boundary range) - check if now date is either within range of yesterday or within range tomorrow  
    } else {
        if (nowHourMinute >= startHourMinute || nowHourMinute <= endHourMinute) {
            System.out.println("Ok, we're within start/end");
        } else {
            System.out.println("Outside start/end");
        }
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ Thanks a lot, really simple and clear using ints as hhmm! \$\endgroup\$
    – yinder
    May 21, 2019 at 15:27
0
\$\begingroup\$

Kotlin version of @sharon-ben-asher answer:

fun LocalDateTime.isInTimeFrame(startHour: Int, startMinute: Int, endHour: Int, endMinute: Int): Boolean {
        val startHourMinute = startHour * 100 + startMinute
        val endHourMinute = endHour * 100 + endMinute

        val nowHourMinute: Int = hour * 100 + minute

        return if (startHourMinute <= endHourMinute) {
            // if range within date - simple between boundaries check
            nowHourMinute in startHourMinute..endHourMinute
        } else {
            // else (cross date boundary range) - check if now date is either within range of yesterday or within range tomorrow
            nowHourMinute >= startHourMinute || nowHourMinute <= endHourMinute
        }
    }

And use it like this:

LocalDateTime.parse(dateString, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")).isInTimeFrame(18,0,6,0)

Is DateTime between 18:00 - 06:00 ?

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.