Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check if a given hour and minute (int's) falls within or is equal to any range within a given set of ranges of times?

Tags:

java

android

Have a pretty simple question here, but would need some logical expertise to solve this. So, I have an API that returns start and end time, for a given time slot in a 24 hour int format such that : start hour would be 4 and start minute would be 30 and end hour would be 5 and end minute would be 30. Please note that the slots returned are in 30 minute increments, such that a given hour would be from 1 to 24, and given minute would be either 30 or 0.

So now I have a set of ranges, such that each range represents a slot that's been taken and I need to identify whether any given time from the API overlaps or falls within one of these slots. (these slots would have hours from 1 to 24 but minutes could differ, could be anything from 0 to 59).

For this my logic so far is:

for (int i = 0; i < ranges.size(); i++) {

    int rangeStartHour = ranges.get(i).getStartTime().getHour();
    int rangeEndHour = ranges.get(i).getEndTime().getHour();
    int rangeStartMinute = ranges.get(i).getStartTime().getMinute();
    int rangeEndMinute = ranges.get(i).getStartTime().getMinute();

    if ((rangeStartHour == 0 && rangeEndHour == 24
            && rangeStartMinute == 0 && rangeEndMinute == 0)) {
        isAvailable = false;
    } else {
        isAvailable = true;
    }
}

I am missing an if in between where I need to identify whether the given time range falls in between or is equal to any of the taken slots to avoid overlap. Any ideas what would be the best way to go about this? I would love to have a very simple and compact solution that always works for this scenario. Open to all suggestions, worth a try! Happy to share more details if need be to clarify this further Thanks in advance!


1 Answers

Say you have a time (hour, minute), a range start time (startHour, startMinute), and a range end time (endHour, endMinute).

To check if the time is within range, lower inclusive, upper exclusive, it is easier if you check whether the time is outside:

if (hour < startHour) {
    // outside
} else if (hour == startHour && minute < startMinute) {
    // outside
} else if (hour > endHour) {
    // outside
} else if (hour == endHour && minute >= endMinute) {
    // outside
} else {
    // inside
}
// combined
if (hour < startHour || hour > endHour ||
        (hour == startHour && minute < startMinute) ||
        (hour == endHour && minute >= endMinute)) {
    // outside
} else {
    // inside
}
// reversed
if (hour >= startHour && hour <= endHour &&
        (hour != startHour || minute >= startMinute) &&
        (hour != endHour || minute < endMinute)) {
    // inside
} else {
    // outside
}

However, with some prep work, it is far easier to combine the hour/minute values into a single minuteOfDay value:

// Prep work
int minuteOfDay = hour * 60 + minute;
int startMinuteOfDay = startHour * 60 + startMinute;
int endMinuteOfDay = endHour * 60 + endMinute;

// Test
if (minuteOfDay >= startMinuteOfDay && minuteOfDay < endMinuteOfDay) {
    // inside
} else {
    // outside
}

It would be even better if you used the ThreeTen Android Backport library:

// Store values as LocalTime instead of hour/minute pairs
LocalTime time = LocalTime.of​(hour, minute);
LocalTime startTime = LocalTime.of​(startHour, startMinute);
LocalTime endTime = LocalTime.of​(endHour, endMinute);

// Test
if (minuteOfDay.compareTo​(startMinuteOfDay) >= 0 && minuteOfDay.compareTo​(endMinuteOfDay) < 0) {
    // inside
} else {
    // outside
}
like image 190
Andreas Avatar answered Oct 20 '25 09:10

Andreas