muhamadto muhamadto - 1 year ago 68
Java Question

Find if two hours ranges overlap regardless of the date

I have a set of two hours ranges

10 PM - 02 AM
01 AM - 08 AM

I want to check if any of them over lap regardless of the the date.

For Example:
The first range could be on 1st and 2nd of Aug while the second range could be 10th of Aug.

This is what I have so far

private Interval createInterval(final OpeningClosingTimesEntry entry) {
LocalDateTime openingHour = LocalDateTime.fromDateFields(entry.getOpenTime());
LocalDateTime closingHour = LocalDateTime.fromDateFields(entry.getCloseTime());
closingHour = closingHour.plusDays(1);
return new Interval(openingHour.toDate().getTime(), closingHour.toDate().getTime());

private Interval adjustSecondIntervalDay(final Interval interval1, final Interval interval2){
if(interval1.getEnd().getDayOfYear() > interval2.getStart().getDayOfYear()){
DateTime start = interval2.getStart().plusDays(1);
DateTime end = interval2.getEnd().plusDays(1);
return new Interval(start, end);
return interval2;

Answer Source

Here's how to do it properly with Java 8's LocalTime. You ask about Joda Time, but you say you use Java 8. Joda Time recommends switching to Java 8 for most cases, and this case is no exception.

Since you don't care about dates, but simply want to know if times overlap or not, you shouldn't use anything like LocalDate or LocalDateTime, but well LocalTime.

To implement your question, I created the isBetween method which check whether two ordered times(-of-day) contain a third time, even if you pass to the next day. So 21 h is between 18 h and 6 h, for instance.

Then, once you have that utility method, you simply have to check if at least one of the two ranges contains bounds of the other.

I leave up to you the decision about bounds themselves (1-2 -- 2-3, for instance). You have the general algorithm, make your own decision for the rest.

package so38810914;

import java.time.LocalTime;
import static java.util.Objects.*;

public class Question {

    public static class LocalTimeRange {

        private final LocalTime from;
        private final LocalTime to;

        public LocalTimeRange(LocalTime from, LocalTime to) {
            requireNonNull(from, "from must not be null");
            requireNonNull(to, "to must not be null");
            this.from = from;
   = to;

        public boolean overlaps(LocalTimeRange other) {
            requireNonNull(other, "other must not be null");
            return isBetween(other.from, this.from,
                    || isBetween(, this.from,
                    || isBetween(this.from, other.from,
                    || isBetween(, other.from,;

        private static boolean isBetween(LocalTime t, LocalTime from, LocalTime to) {
            if (from.isBefore(to)) { // same day
                return from.isBefore(t) && t.isBefore(to);
            } else { // spans to the next day.
                return from.isBefore(t) || t.isBefore(to);

    public static void main(String[] args) {
        test( 0,  1,     2,  3,    false);
        test( 2,  3,     0,  1,    false);
        test( 0,  3,     1,  2,    true);
        test( 1,  2,     0,  3,    true);
        test( 0,  2,     1,  3,    true);
        test(12, 18,    15, 21,    true);
        test(18,  6,    21,  3,    true);
        test(21,  3,     0,  6,    true);
        test(21,  0,     3,  6,    false);


    private static void test(int from1, int to1, int from2, int to2, boolean overlap) {
        LocalTimeRange range1 = new LocalTimeRange(LocalTime.of(from1, 0), LocalTime.of(to1, 0));
        LocalTimeRange range2 = new LocalTimeRange(LocalTime.of(from2, 0), LocalTime.of(to2, 0));
        boolean test = (range1.overlaps(range2)) == overlap;
        System.out.printf("[%2d-%2d] - [%2d-%2d] -> %-5b: %s%n", from1, to1, from2, to2, overlap, test?"OK":"Not OK");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download