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

后端 未结 2 1800
忘了有多久
忘了有多久 2021-01-20 03:27

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

相关标签:
2条回答
  • 2021-01-20 03:59

    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;
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-20 04:18

    LocalTime is exactly that – a time.
    It doesn't store a date, so your question is fundamentally impossible.

    If you want to count hours as they wrap around days, you need to get a date as well, and you need to beware of DST.

    0 讨论(0)
提交回复
热议问题