Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

In this short tutorial, we’ll learn how to get the start and the end of a day in Java, using simple, straightforward examples for different scenarios.

We’ll be using the Java’s 8 Date/Time API to build these examples.

In case you want to read a little bit more about Java’s 8 Date and Time library before proceeding, you can get started here.

2. From a LocalDate Object

First of all, let’s see how we can get the start or end of a day given to us as a LocalDate object, such as:

LocalDate localDate = LocalDate.parse("2018-06-23");

2.1. atStartOfDay()

The simplest way of getting a LocalDateTime representing the beginning of a particular day is by using the atStartOfDay() method:

LocalDateTime startOfDay = localDate.atStartOfDay();

This method is overloaded, thus in case we want to get a ZonedDateTime from it, we can do so by specifying the ZoneId:

ZonedDateTime startOfDay = localDate.atStartOfDay(ZoneId.of("Europe/Paris"));

2.2. of()

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

LocalTime offers the following static fields: MIDNIGHT (00:00), MIN (00:00), NOON (12:00), and MAX(23:59:59.999999999).

Therefore, if we want to get the end of the day, we’d use the MAX value.

Let’s try it out, but with a different method.

2.3. atTime()

This method is overloaded, allowing us to specify the desired time using hours, minutes, seconds or even nanoseconds.

In this case, anyway, we’ll use the LocalTime‘s MAX field as the argument to get the last moment of the given day:

LocalDateTime startOfDay = localDate.atTime(LocalTime.MAX);

2.4. atDate()

This example is quite similar to the previous ones, but this time, we’ll use the atDate() method of a LocalTime object, passing the LocalDate as the argument:

LocalDateTime endOfDate = LocalTime.MAX.atDate(localDate);

3. From a LocalDateTime Object

It almost goes without saying that we can get the LocalDate from it, and then use any of the methods of section 2 to get the end or start of day from it:

LocalDateTime localDateTime = LocalDateTime
  .parse("2018-06-23T05:55:55");
LocalDateTime endOfDate = localDateTime
  .toLocalDate().atTime(LocalTime.MAX);

But in this section, we’ll analyze one other method to obtain the object with its time section set to the start or end of the day, directly from another given LocalDateTime object.

3.1. with()

All classes implementing the Temporal interface can use this method.

In this case, we’ll use the signature of the method which takes a TemporalField (notably, one of the ChronoField Enum values) and a long argument as the new value of the field:

LocalDateTime endOfDate = localDateTime.with(ChronoField.NANO_OF_DAY, LocalTime.MAX.toNanoOfDay());

4. From a ZonedDateTime Object

If we’re given a ZonedDateTime, we can use the with() method since it implements the Temporal interface as well:

ZonedDateTime startofDay = zonedDateTime.with(ChronoField.HOUR_OF_DAY, 0);

5. Conclusion

To sum up, we’ve analyzed a lot of different ways of getting the start and end of a day in Java for many different case scenarios.

Finally, we’ve learned about the insights of Java’s 8 Date and Time library classes and got familiar with many of its classes and interfaces.

As always, all the examples can be accessed in our GitHub repository.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are closed on this article!