Symptom
When specifying a date range for a search, the result I expected didn't show up. I entered the same day as the beginning and the end of the search range. This was the same date found in my expected result.
Problem
The date selector I was using picked some arbitrary time of day. It subsequently compared my potential results to that exact time. Since my expected result was merely on the same day, it did not show up.
Solution
You will want to set the time of the start day to be the start of the day. Likewise, you will want the end day to be the end of the day. This way, your search will include every result in the start day and the end day. The code below will accept any Date object set the time to the beginning or end of the day.
public static Date beginningOfDay(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.HOUR_OF_DAY, 0 - calendar.get(Calendar.HOUR_OF_DAY));
calendar.add(Calendar.MINUTE, 0 - calendar.get(Calendar.MINUTE));
calendar.add(Calendar.SECOND, 0 - calendar.get(Calendar.SECOND));
calendar.add(Calendar.MILLISECOND, 0 - calendar.get(Calendar.MILLISECOND));
return calendar.getTime();
}
public static Date endOfDay(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.HOUR_OF_DAY, 23 - calendar.get(Calendar.HOUR_OF_DAY));
calendar.add(Calendar.MINUTE, 59 - calendar.get(Calendar.MINUTE));
calendar.add(Calendar.SECOND, 59 - calendar.get(Calendar.SECOND));
calendar.add(Calendar.MILLISECOND, 999 - calendar.get(Calendar.MILLISECOND));
return calendar.getTime();
}