I have the following code
private boolean checkIfTimeInBetweenRegardlessOfDate(long timeOne, long timeTwo) {
final Calendar firstCalendar = Calendar.getInstance();
firstCalendar.setTimeInMillis(timeOne);
final Calendar calendarCurrentTime = mCalendar;
final Calendar secondCalendar = Calendar.getInstance();
secondCalendar.setTimeInMillis(timeTwo);
final Calendar calendarOneToCompare = Calendar.getInstance();
calendarOneToCompare.setTimeInMillis(calendarCurrentTime.getTimeInMillis());
calendarOneToCompare.set(Calendar.HOUR_OF_DAY, firstCalendar.get(Calendar.HOUR_OF_DAY));
calendarOneToCompare.set(Calendar.MINUTE, firstCalendar.get(Calendar.MINUTE));
final Calendar calendarTwoToCompare = Calendar.getInstance();
calendarTwoToCompare.setTimeInMillis(calendarCurrentTime.getTimeInMillis());
calendarTwoToCompare.set(Calendar.HOUR_OF_DAY, secondCalendar.get(Calendar.HOUR_OF_DAY));
calendarTwoToCompare.set(Calendar.MINUTE, secondCalendar.get(Calendar.MINUTE));
if ((calendarTwoToCompare.getTime().toString())
.compareTo(calendarOneToCompare.getTime().toString()) < 0) {
calendarTwoToCompare.add(Calendar.DATE, 1);
calendarCurrentTime.add(Calendar.DATE, 1);
}
return (calendarOneToCompare.compareTo(calendarCurrentTime) <= 0
&& calendarCurrentTime.compareTo(calendarTwoToCompare) <= 0);
}
So this question has popped up a few times before on SO. Nobody's code seems to function for all of the cases.
Let's say the Current_Hour
is 8pm
. It needs to work for these cases:
1) return true if Current_Hour
is between 6:00pm
and 11:15pm
2) return true if Current_Hour
is between 6:00pm
and 2:00am
3) return false if Current_Hour
is between 3:45pm
and 6:10pm
If Current_Hour
is 2am
, then the following cases need to be met:
4) return true if Current_Hour
is between 1:00am
and 3:30am
5) return false if Current_Hour
is between 7:00am
and 12:02pm
I have struggled with this all day and no matter what I do, I can satiate all but 1 or two of those above requirements.
This needs to work regardless of the date--although it will be needed for case #2.
Any help would be appreciated. I'm going crazy.