Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain the start time and end time of a day?

Tags:

java

date

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.

like image 306
kalman03 Avatar asked Apr 25 '12 01:04

kalman03


People also ask

How to get the start and the end of a day using Java?

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);

How do you set the time to the start of the day in Java?

ofInstant( instant , zoneId ); To get the first moment of the day go through the LocalDate class and its atStartOfDay method. ZonedDateTime zdtStart = zdt. toLocalDate().

What is zoned date time?

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.

How do I find my LocalDate date?

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.


1 Answers

Java 8


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

  • DateUtils.atStartOfDay
  • DateUtils.atEndOfDay

It is in the Maven Central Repository at:

<dependency>   <groupId>com.github.rkumsher</groupId>   <artifactId>utils</artifactId>   <version>1.3</version> </dependency> 

Java 7 and Earlier


With Apache Commons

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); } 

Without Apache Commons

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(); } 
like image 58
RKumsher Avatar answered Sep 20 '22 15:09

RKumsher