My lib Time4J offers following solution for the subtraction problem:
ClockInterval fullDay = ClockInterval.between(PlainTime.of(0), PlainTime.of(24));
ClockInterval slot = ClockInterval.between(PlainTime.of(13, 0), PlainTime.of(19, 0));
IntervalCollection<PlainTime> icoll = IntervalCollection.onClockAxis().plus(fullDay);
List<ChronoInterval<PlainTime>> result = icoll.minus(slot).getIntervals();
The resulting list of half-open intervals (with open end) can be easily iterated through and gives the expected result {[T00:00/T13:00), [T19:00/T24:00)}. Every result interval can be converted to a standard ClockInterval
, too. There are also various methods to print such intervals in a localized way. Furthermore, you might find the class DayPartitionBuilder
interesting which allows to connect weekdays and time schedules in streaming, see the example given in the documentation.
About compatibility with java.time
:
- The between()-methods of
ClockInterval
also accept instances of java.time.LocalTime
.
- Every instance of
PlainTime
can be converted back to LocalTime
by help of the method toTemporalAccessor()
with the exception of the value 24:00 which exists in Time4J but not in java.time.LocalTime
.