A quick look at the Interval API gives this (UNTESTED):
// SUPPOSED: the big interval is "bigInterval"; the list is "intervals" // Intervals returned List<Interval> ret = new ArrayList<>(); Interval gap, current, next; // First, compute the gaps between the elements in the list current = intervals.get(0); for (int i = 1; i < intervals.size(); i++) { next = intervals.get(i); gap = current.gap(next); if (gap != null) ret.add(gap); current = next; } // Now, compute the time difference between the starting time of the first interval // and the starting time of the "big" interval; add it at the beginning ReadableInstant start, end; start = bigInterval.getStart(); end = intervals.get(0).getStart(); if (start.isBefore(end)) ret.add(0, new Interval(start, end)); // // finally, append the time difference between the ending time of the last interval // and the ending time of the "big" interval // next still contains the last interval start = next.getEnd(); end = bigInterval.getEnd(); if (start.isBefore(end)) ret.add(new Interval(start, end)); return ret;
source share