To get the end time of the day.
Date now = new Date();
now.setHours(23);
now.setMinutes(59);
now.setSeconds(59);
To get the stat time of the day.
Date now = new Date();
now.setHours(00);
now.setMinutes(00);
now.setSeconds(00);
Date.set()
methods are deprecated. Use Calendar
instead.
Calendar cal = Calendar.getInstance();
Calendar start = new GregorianCalendar(cal.get(Calendar.YEAR),
cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
Calendar end = new GregorianCalendar(cal.get(Calendar.YEAR),
cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
CLARIFICATION: A day does not end at 23:59:59. The above code just creates two Calendar
instances, one set at 0:00:00 and the other one at 23:59:59 of the current day.
Calendar
instances exactly as the OP indicated; the first one set at 0:00:00 and the other at 23:59:59. Declaring either of them as the "boundaries" of a certain day is completely wrong. Nonetheless, the OP can use any of the 7 constructor the GregorianCalendar
class provides.
Commented
May 17, 2016 at 9:06
java.tome.LocalDate
class instead, that doesn't have a time notion. And from there, if you want a time, then use something likeLocalDate.now().atStartOfDay()