Ok, so I'm ripping my hair out over this.
I have two 3 GregorianCalendar objects. Due to reasons unknown, the .after() and .before() methods are not working correctly all the time, so I'm having to make a method my own to get some sort of consistency.
Basically, I'm just wanting to check if a Date is between (inclusively) a range of dates.
After trying a bunch of different things (all with complete failure), I tried to make the most concise set of conditional statements I could. But, it still doesn't work. Can anyone find the logic problem in this?
public boolean withinTimeFrame(GregorianCalendar g) { int year = g.get(GregorianCalendar.YEAR); int month = g.get(GregorianCalendar.MONTH); int day = g.get(GregorianCalendar.DAY_OF_MONTH); int yearS = StartDate.get(GregorianCalendar.YEAR); int monthS = StartDate.get(GregorianCalendar.MONTH); int dayS = StartDate.get(GregorianCalendar.DAY_OF_MONTH); int yearE = EndDate.get(GregorianCalendar.YEAR); int monthE = EndDate.get(GregorianCalendar.MONTH); int dayE = EndDate.get(GregorianCalendar.DAY_OF_MONTH); if((year==yearS && month==monthS && day==dayS)||(year==yearE && month==monthE && day==dayE)) return true; if(year>yearS) { if(year<yearE) return true; else if(year==yearE) { if(month<monthE) return true; else if(month==monthE) { if(day<=dayE) return true; else return false; } else return false; } else return false; } else if(year==yearS) { if(year<yearE) return true; else if(year==yearE) { if(month>monthS) { if(month<monthE) return true; else if(month==monthE) { if(day<=dayE) return true; else return false; } else return false; } else if(month==monthS) { if(day>dayS) { if(month<monthE) return true; else if(month==monthE) { if(day<=dayE) return true; else return false; } else return false; } else if(day==dayS) return true; else return false; } else return false; } else return false; } else return false; }
For example, if I have the following 2 inputs:
Input 1:
StartDate = September 7, 2011
EndDate = November 7, 2011
g = October 6, 2011
Input 2:
StartDate = September 7, 2011
EndDate = November 7, 2011
g = November 4, 2011
So, if g is between (inclusively) StartDate and EndDate, the method should return true. Otherwise, it should return false.
At the moment, I know at least one of these (if not both) is returning true. I'm attempting to isolate the problem.
EDIT:
It is returning false for both inputs.