public static Date setDateToEndDay(final Date date) { Calendar lcal = Calendar.getInstance(); lcal.setTime(date); lcal.set(Calendar.HOUR, 23); lcal.set(Calendar.MINUTE, 59); lcal.set(Calendar.SECOND, 0); lcal.set(Calendar.MILLISECOND, 0); return lcal.getTime(); }
As you can see, it sets the time to be 23H59. Being used to the quirks of the Calendar class, and having already fallen to the trap, I immediately saw the bug that would, sometimes, change the day to the next one.
If you take a look at the javadoc for the HOUR field, it says the following:
"Field number for get and set indicating the hour of the morning or afternoon. HOUR is used for the 12-hour clock (0 - 11). Noon and midnight are represented by 0, not by 12. E.g., at 10:04:15.250 PM the HOUR is 10."
So the field should have a value between 0 and 11. If the time part of your date is in the morning, everything will work as expected. But If you have a time in the afternoon, or even at noon, something will change. Something small actually: the AM/PM field will be set to PM. The result is that setting the HOUR field to 23 will bring you to the next day at 11...
The correct field to use for this situation is called HOUR_OF_DAY. Be bitten once, and you will remember it.