75. Start and end of a day

In JDK 8, trying to find the start/end of a day can be accomplished in several ways.

Let's consider a day expressed via LocalDate:

LocalDate localDate = LocalDate.of(2019, 2, 28);

The solution to finding the start of the day February 28, 2019, relies on a method named atStartOfDay(). This method returns LocalDateTime from this date at the time of midnight, 00:00:

// 2019-02-28T00:00LocalDateTime ldDayStart = localDate.atStartOfDay();

Alternatively, the solution can use the of(LocalDate date, LocalTime time) method. This method combines the given date and time into LocalDateTime. So, if the passed time is LocalTime.MIN (the time of midnight at the start of the day) then the result will be as follows:

// 2019-02-28T00:00 ...

Get Java Coding Problems now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.