How to obtain the start time and end time of a day?
code like this is not accurate:
private Date getStartOfDay(Date date) { Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DATE); calendar.set(year, month, day, 0, 0, 0); return calendar.getTime(); } private Date getEndOfDay(Date date) { Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DATE); calendar.set(year, month, day, 23, 59, 59); return calendar.getTime(); }
It is not accurate to the millisecond.
Another way in which we can achieve the same result is by using the of() method, providing a LocalDate and one of the LocalTime's static fields: LocalDateTime startOfDay = LocalDateTime. of(localDate, LocalTime. MIDNIGHT);
ofInstant( instant , zoneId ); To get the first moment of the day go through the LocalDate class and its atStartOfDay method. ZonedDateTime zdtStart = zdt. toLocalDate().
ZonedDateTime is an immutable representation of a date-time with a time-zone. This class stores all date and time fields, to a precision of nanoseconds, and a time-zone, with a zone offset used to handle ambiguous local date-times.
1. LocalDate. LocalDate is an immutable class that represents Date with default format of yyyy-MM-dd. We can use now() method to get the current date.
public static Date atStartOfDay(Date date) { LocalDateTime localDateTime = dateToLocalDateTime(date); LocalDateTime startOfDay = localDateTime.with(LocalTime.MIN); return localDateTimeToDate(startOfDay); } public static Date atEndOfDay(Date date) { LocalDateTime localDateTime = dateToLocalDateTime(date); LocalDateTime endOfDay = localDateTime.with(LocalTime.MAX); return localDateTimeToDate(endOfDay); } private static LocalDateTime dateToLocalDateTime(Date date) { return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()); } private static Date localDateTimeToDate(LocalDateTime localDateTime) { return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()); }
Update: I've added these 2 methods to my Java Utility Classes here
It is in the Maven Central Repository at:
<dependency> <groupId>com.github.rkumsher</groupId> <artifactId>utils</artifactId> <version>1.3</version> </dependency>
public static Date atEndOfDay(Date date) { return DateUtils.addMilliseconds(DateUtils.ceiling(date, Calendar.DATE), -1); } public static Date atStartOfDay(Date date) { return DateUtils.truncate(date, Calendar.DATE); }
public Date atEndOfDay(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.HOUR_OF_DAY, 23); calendar.set(Calendar.MINUTE, 59); calendar.set(Calendar.SECOND, 59); calendar.set(Calendar.MILLISECOND, 999); return calendar.getTime(); } public Date atStartOfDay(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); return calendar.getTime(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With