1

I have this programming exercise which looks for overlapping date ranges.

So far this is what I've done:

  private static final SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd, yyyy");
private static final Date invalidDate = new Date(0);

private static final Date fromString( String spec ) {
    try {
        return dateFormat.parse( spec );
    } catch( ParseException dfe ) {
        return invalidDate;
    }
}

public static void main(String[] args) {

    Date [] randomDates = {
            fromString("Aug 28, 2014"), 
            fromString("Sep 1, 2014"), 
            fromString("Aug 30, 2014"), 
            fromString("Sep 3, 2014"), 
            fromString("Sep 5, 2014"),
            fromString("Sep 7, 2014")
        };

    for( Date date: randomDates ) {
        print( date );
    }

}

private static final void print( Date date ) {
    if( date == invalidDate ) {
        System.out.println("Invalid date");
    } else {
        System.out.println( dateFormat.format( date ) );
    }
}

But I can't seem to figure out the overlapping of dates. Or I don't still get how to find the overlapping dates. Any ideas? Your help will be truly appreciated.

  • possible duplicate of [Determine Whether Two Date Ranges Overlap](http://stackoverflow.com/questions/325933/determine-whether-two-date-ranges-overlap) – PM 77-1 Jan 07 '15 at 07:02
  • One approach would be to get the absolute milliseconds and then doing a Math on the ranges. Note: Absolute milliseconds are calculated from 1970. – Sid Jan 07 '15 at 07:10

1 Answers1

1

For checking overlapping date ranges you can use JodaTime

DateTime currentdate1 = DateTime.now(); //first Date range
DateTime endDate1 = now.plusHours(10);

DateTime currentdate2 = now.plusDays(5); //second Date range
DateTime endDate2 = now.plusDays(6);

Interval interval1 = new Interval( currentdate1, endDate1  );
Interval interval2 = new Interval( currentdate2, endDate2  );

System.out.println( interval1.overlaps( interval2 ) );

Edit:-

Since you do not want to use external library, you can use .compareTo() of Date and try to implement the following condition:-

( currentdate1 <= endDate2 and currentdate2 <= endDate1 )

Example:-

if( (currentdate1.compareTo(endDate2)<=0) && (currentdate2.compareTo(endDate1)<=0) ){
//ranges overlap
}

Since you want to compare a single date and check if it is present in the range specified you can do something like this:-

| Daterange start------Your Date------ DateRange ends|

Hence:-

for(Date date:randomDates){
//check if the date in in the range
if( (DateRangeStart.compareTo(date)<=0) && (date.compareTo(DateRangeEnd)<=0)){
//date overlaps
System.out.println(dateFormat.format(date));
}
}

Edit:- Updating answer to include solution to the problem as discussed in chat

outerloop: for (int i = 0; i < (dates.length - 1); i = i + 2) {
            Date currentdate1 = dates[i];
            Date endDate1 = dates[i + 1];

            for (int j = i + 2; j < dates.length - 1; j = j + 2) {
                Date currentdate2 = dates[j];
                Date endDate2 = dates[j + 1];

                if ((currentdate1.compareTo(endDate2) <= 0)
                        && (currentdate2.compareTo(endDate1) <= 0)) {
                    System.out.println("Overlapping found:-");
                    print(currentdate2);
                    print(endDate2);
                    break outerloop;

                }
            }
        }
Mustafa sabir
  • 4,130
  • 1
  • 19
  • 28
  • This is great but I don't want to use any external libraries. And given the problem I'm solving, I need to display the overlapped date events from the array –  Jan 07 '15 at 07:15
  • Check updated answer, you can iterate over you array and check if each date is in the range, i have just printed the dates, you can do something more like adding them into another array for example – Mustafa sabir Jan 07 '15 at 07:53
  • do I need to create DateRangeStart and DateRangeEnd class? –  Jan 07 '15 at 07:57
  • No they are objects of `Date` representing the specified date range. For example if you want dates between 1st jan and 5th jan, you can write `DateRangeStart=fromString("Jan 01, 2015"); DateRangeEnd=fromString("Jan 05, 2015");` – Mustafa sabir Jan 07 '15 at 08:18
  • but when I tried this, it just displays all the items from the array? –  Jan 07 '15 at 08:54
  • Maybe then your range is too broad, shorten your range :) – Mustafa sabir Jan 07 '15 at 10:33
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/68381/discussion-between-betty-barnes-and-mustafa-sabir). –  Jan 08 '15 at 02:18
  • I did something like this: Date DateRangeStart = fromString("Aug 28, 2014"); Date DateRangeEnd = fromString("Sep 7, 2014"); and it displays all dates –  Jan 08 '15 at 02:22