How to check a time period is overlapping another time period in the same day.
For example,
Java Program to find overlapping intervals among a given set of intervals. In this approach, we are going to take a count array and for each given interval start time we will do count[startTime]++ and for end time, do count[endTime]--. sum > 2 it means there is overlapping intervals.
1) Sort all intervals in increasing order of start time. This step takes O(nLogn) time. 2) In the sorted array, if start time of an interval is less than end of previous interval, then there is an overlap.
Overlap = min(A2, B2) - max(A1, B1) + 1. In other words, the overlap of two integer intervals is a difference between the minimum value of the two upper boundaries and the maximum value of the two lower boundaries, plus 1.
Time zone overlap means that two or more time zones intersect each other. In the context of working remotely, this means that people in two different time zones can theoretically be working at the same time.
There is a simple solution, expressed here as a utility method:
public static boolean isOverlapping(Date start1, Date end1, Date start2, Date end2) { return start1.before(end2) && start2.before(end1); }
This code requires there to be at least one millisecond to be shared between the two periods to return true
.
If abutting time periods are considered to "overlap" (eg 10:00-10:30 and 10:30-11:00) the logic needs to be tweaked ever so slightly:
public static boolean isOverlapping(Date start1, Date end1, Date start2, Date end2) { return !start1.after(end2) && !start2.after(end1); }
This logic more often comes up in database queries, but the same approach applies in any context.
Once you realise just how simple it is, you at first kick yourself, then you put it in the bank!
( startA.isBefore( stopB ) ) && ( stopA.isAfter( startB ) )
LocalTime
If you really want to work with a generic time-of-day without the context of a date and time zone, use the LocalTime
class.
LocalTime startA = LocalTime.of( 7 , 0 ); LocalTime stopA = LocalTime.of( 10 , 30 ); LocalTime startB = LocalTime.of( 10 , 0 ); LocalTime stop2B = LocalTime.of( 11 , 30 );
Validate the data, being sure the ending is after the beginning (or equal). A briefer way of saying that is “beginning is not after ending”.
Boolean validA = ( ! startA.isAfter( stopA ) ) ; Boolean validB = ( ! startB.isAfter( stop2B ) ) ;
Per this Answer by Meno Hochschild, using the Half-Open approach to defining a span of time where the beginning is inclusive while the ending is exclusive, we can use this logic:
(StartA < EndB) and (EndA > StartB)
Boolean overlaps = ( ( startA.isBefore( stopB ) ) && ( stopA.isAfter( startB ) ) ) ;
Note that LocalTime
is constrained to a single generic 24-hour day. The times cannot go past midnight, cannot wrap around into another. There are no other days to consider. Validate your inputs to verify the beginning time comes before the end, or they are equal (if that suits your business rules).
if( stopA.isBefore( startA ) ) { … handle error } if( stopB.isBefore( startB ) ) { … handle error }
ZonedDateTime
If you want to test actual moments on the timeline, you must adjust these time-of-day objects into the context of dates and a time zone. Apply a ZoneId
to get a ZonedDateTime
object.
ZoneId z = ZoneId.of( "America/Montreal" ); LocalDate today = LocalDate.now( z ); ZonedDateTime zdt = ZonedDateTime.of( today , startA , z);
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With