Compare unlimited time slots with joda-lib

Is it possible to determine the coincidence of two unlimited intervals (intervals with one boundary at infinity)?

I tried this (and other similar options):

Instant now = new Instant(new Date().getTime()); Interval i2 = new Interval(now, (ReadableInstant) null); Interval i1 = new Interval(now, (ReadableInstant) null); boolean overlapping = i2.overlaps(i1); 

But according to the docs, using null as the second parameter means β€œnow” instead of β€œinfinity”.

EDIT : I found this answer on the mailing list, so with Joda this seems impossible, Now I'm looking for alternatives.

+6
source share
3 answers

If both intervals start with t = -∞ or if both intervals end with t = +∞ , they will always overlap, regardless of the start date.

If interval A starts with t = -∞ , and interval B starts with t = +∞ , they overlap iff
A.start > B.start .

+4
source

Hacky Solution:

 /** * Checks if two (optionally) unbounded intervals overlap each other. * @param aBeginn * @param aEnde * @param bBeginn * @param bEnde * @return */ public boolean checkIfOverlap(LocalDate aBeginn,LocalDate aEnde, LocalDate bBeginn,LocalDate bEnde){ if(aBeginn == null){ //set the date to the past if null aBeginn = LocalDate.now().minusYears(300); } if(aEnde == null){ aEnde = LocalDate.now().plusYears(300); } if(bBeginn == null){ bBeginn = LocalDate.now().minusYears(300); } if(bEnde == null){ bEnde = LocalDate.now().plusYears(300); } if(aBeginn != null && aEnde != null && bBeginn != null && bEnde != null){ Interval intervalA = new Interval(aBeginn.toDateTimeAtStartOfDay(),aEnde.toDateTimeAtStartOfDay()); Interval intervalB = new Interval(bBeginn.toDateTimeAtStartOfDay(),bEnde.toDateTimeAtStartOfDay()); if(intervalA.overlaps(intervalB)){ return true; } } else{ return false; } return false; } 
+2
source

I would recommend using Range<DateTime> ( from Guava ), which should provide all the build options and comparison functions you need.

 Range<DateTime> r1= Range.atLeast(DateTime.now()); Range<DateTime> r2 = Range.atLeast(DateTime.now()); boolean overlap = r1.isConnected(r2) && !r1.intersection(r2).isEmpty(); 
+1
source

Source: https://habr.com/ru/post/897613/


All Articles