# Comparing Two Dates

• June 27th, 2011, 11:06 AM
aussiemcgr
Comparing Two Dates
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?

Code java:

```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.
• June 27th, 2011, 11:26 AM
Norm
Re: Comparing Two Dates
Could you use this value: time as UTC milliseconds from the epoch
A long value for the three dates and then just compare them.
• June 27th, 2011, 12:23 PM
copeg
Re: Comparing Two Dates
As Norm suggested, just get the time in milliseconds (Calendar.getTimeInMillis() - the Date class has a similar method) and compare the returned long value. Seems a much easier alternative
• June 27th, 2011, 02:09 PM
aussiemcgr
Re: Comparing Two Dates
This was actually my fault, not the code's. I was checking one direction, but not the other.

For example, if you had two date ranges:
Oct 5, 2011 to Dec 10, 2011
Nov 1, 2011 to Nov 10, 2011

The first would not conflict with the second, but the second would conflict with the first. I needed the second to conflict with the first AND the first to conflict with the second. I have it working as far as I know.