I just announced the new Spring 5 modules in REST With Spring:
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");
The simplest way of getting a LocalDateTime representing the beginning of a particular day is by using the atStartOfDay() method:
LocalDateTime startOfDay = localDate.atStartOfDay();
ZonedDateTime startOfDay = localDate.atStartOfDay(ZoneId.of("Europe/Paris"));
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.
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);
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.
All classes implementing the Temporal interface can use this method.
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);
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.