I am trying to compare 2 days (actually 3). In my use case, I have 3 dates that I need to compare. One of them is StartSleepingTime, StopSleepingTime and Now. My application monitors user activity levels, and I have a schedueld task that runs every half hour to check if there is NOW between the user StartSleepingTime and StopSleepingTime to make sure that the activity monitoring service is stopped during this interval. Since the user sets StartSleepingTime and StopSleepingTime on the first login, when I start the scheduled tasks, both will be in the "past" compared to "NOW". Currently, I am trying to extract only hourly and minute information from three dates and make such a comparison:
public static boolean compareHrsAndMintsOnly(Date startSH, Date now, Date stopSH) {
boolean isNowWithinSleepingTime = false;
Calendar startSHCalendar = Calendar.getInstance();
startSHCalendar.setTime(startSH);
Calendar nowCalendar = Calendar.getInstance();
nowCalendar.setTime(now);
Calendar stopSHCalendar = Calendar.getInstance();
stopSHCalendar.setTime(stopSH);
int startSHhour = startSHCalendar.get(Calendar.HOUR_OF_DAY);
int startSHmin = startSHCalendar.get(Calendar.MINUTE);
int nowHour = nowCalendar.get(Calendar.HOUR_OF_DAY);
int nowMin = nowCalendar.get(Calendar.MINUTE);
int stopSHhour = stopSHCalendar.get(Calendar.HOUR_OF_DAY);
int stopSHmin = stopSHCalendar.get(Calendar.MINUTE);
if ((startSHhour > nowHour && startSHmin > nowMin) && (nowHour < stopSHhour && nowMin < stopSHmin)) {
isNowWithinSleepingTime = true;
}else {
isNowWithinSleepingTime = false;
}
return isNowWithinSleepingTime;
}
- . , , . , !