java - Workout Gaps between several date entries -
i trying create mechanism requires user enter have been doing last 5 years
- date range must covered (start date 01/08/2009 - end date 01/08/2014)
- up ten entries allowed
- overlapping allowed
- no gaps not allowed
currently order entries start date, verify both ranges covered. gaps between entries compare 2 dates @ time, entry , i+1 , compare start date of later start date earlier start dates end date. find difficult put words think small diagram helps describe problem
start x---------------------------------------y b x----------y c x--------y d x-----y e x----------y f x----------y end
here option e fail because start date after d's end date, use while loop add day gap everyday start date after end date, trigger validation, though dates have been covered entry a.i using java in text , pseudo code great more logic im lost ,
if gaps between dates grateful.
the logic of simple, you're looking @ backwards.
for every day in requested time period, day covered 1 of date entries? if find day isn't covered, there's gap , can stop checking , send error user - don't keep checking days unless want notify user of specifics of gaps identify, "fail early" find day isn't covered.
so, in pseudo-code...
foreach (day d in daterange) { foreach (userdaterange u in userdateranges) { //check if date range (u) covers day in question (d) if (d > u.start && d < u.end) { //we're done checking day because it's covered @ least 1 range covered = true; } } if (!covered) return false; } return true; //because if got far, every day "covered"
so... loops through days, , each day, checks date ranges. if 1 found covers day, can go next day.
there's lots of room optimization here. example, when find date range covers 1 day, can skip end of date range, don't need check rest of days in range. loops complex conditions work here. know can put whatever condition want in there, right?
(int x = 0; covered == false; x++ ) //perfectly valid
Comments
Post a Comment