Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JodaTime - Check if LocalTime is after now and now before another LocalTime

I am trying to check if the currenttime is after a startLocalTime and before another endLocalTime which is the start time plus 11 hours. This works fine if the start time is 11:00 (end time will be 22:00). However when I try to compare a start time of 16:00 and an end time of 03:00, currentTime.isBefore(endTime) will never be logged, but it should be.

LocalTime currentTime = LocalTime.now(); //21:14
LocalTime startTime = new LocalTime( 16, 00, 0, 0);
LocalTime endTime = startTime.plusHours(11); //03:00 Midnight

    if(currentTime.isAfter(startTime)){
        Log.v("Test", "After startTime");
    }

    if(currentTime.isBefore(endTime)){
        Log.v("Test", "Before endTime");
    }

Any ideas how to solve this problem?

like image 705
Jef Avatar asked Dec 16 '22 00:12

Jef


1 Answers

Basically you need to check whether your startTime and endTime are inverted (i.e. if endTime is before startTime). If it is, that must be a "night-time" interval, and you should just treat it as if they were the other way round, and invert the result.

public boolean isWithinInterval(LocalTime start, LocalTime end, LocalTime time) {
    if (start.isAfter(end)) {
        return !isWithinInterval(end, start, time);
    }
    // This assumes you want an inclusive start and an exclusive end point.
    return start.compareTo(time) <= 0 &&
           time.compareTo(end) < 0;
}

Now the only oddity here is that as shown, the start time is normally inclusive and the end time is exclusive - whereas if we invert the result (and arguments) we have an inclusive end time and an exclusive start time. So you might want to handle the cases explicitly instead:

public boolean isWithinInterval(LocalTime start, LocalTime end, LocalTime time) {
    if (start.isAfter(end)) {
        // Return true if the time is after (or at) start, *or* it's before end
        return time.compareTo(start) >= 0 ||
               time.compareTo(end) < 0;
    } else {
        return start.compareTo(time) <= 0 &&
               time.compareTo(end) < 0;
    }
}

(How you choose which target and argument you use for compareTo is up to you, of course. There are multiple ways of writing effectively the same code.)

Short but complete example:

import org.joda.time.LocalTime;

public class Test {
    public static void main(String[] args) {
        LocalTime morning = new LocalTime(6, 0, 0);
        LocalTime evening = new LocalTime(18, 0, 0);
        LocalTime noon = new LocalTime(12, 0, 0);
        LocalTime midnight = new LocalTime(0, 0, 0);
        System.out.println(isWithinInterval(morning, evening, noon)); // true
        System.out.println(
            isWithinInterval(morning, evening, midnight)); // false
        System.out.println(
            isWithinInterval(evening, morning, noon)); // false
        System.out.println(
            isWithinInterval(evening, morning, midnight)); // true
    }

    public static boolean isWithinInterval(LocalTime start, 
                                           LocalTime end,
                                           LocalTime time) {
        if (start.isAfter(end)) {
            // Return true if the time is after (or at) start,
            // *or* it's before end
            return time.compareTo(start) >= 0 ||
                time.compareTo(end) < 0;
        } else {
            return start.compareTo(time) <= 0 &&
                time.compareTo(end) < 0;
        }
    }
}
like image 123
Jon Skeet Avatar answered Dec 17 '22 15:12

Jon Skeet