Getting the Start and the End of a Day in Java

Learn to get the start and the end of a date using Java date API, e.g., LocalDateTime and ZonedDateTime.

We may require to get this information in many cases. For example, we have to filter the events that occurred at different times in a single day based on event timestamps.

1. Overview

Theoretically, the start of any day is the time of midnight, '00:00' when this day starts. Similarly, the end of the day is the time '23:59:59.999999999' just before midnight.

In Java, we have these constants to represent these time instants every day:

  • LocalTime.MIN (and LocalTime.MIDNIGHT) : is the time of midnight at the start of the day.
  • LocalTime.MAX : is the time just before midnight at the end of the day.

Additionally, LocalDate.atStartOfDay() method is available that combines given LocalDate with the time of midnight to create a LocalDateTime at the start of this date.

There is no similar method to get the end of the day straightforwardly.

2. Getting the Start of Day

As discussed above, we can get the start of the day using LocalTime.MIN constant and atStartOfDay() method in the following ways:

Get the start of a date using atStartOfDay() method in the local timezone as well as in a specific timezone.

//The date for which start of day needs to be found
LocalDate localDate = LocalDate.now();

//Local date time
LocalDateTime startOfDay = localDate.atStartOfDay();

//Current zone
ZonedDateTime startOfDayInZone = localDate
      .atStartOfDay(ZoneId.systemDefault());

//Specific Zone
ZonedDateTime startOfDayInEurope = localDate
      .atStartOfDay(ZoneId.of("Europe/Paris"));

Get the beginning of a date using LocalTime.MIN method in the local timezone as well as in a particular timezone.

LocalDate localDate = LocalDate.now();

//Local date time
LocalDateTime startOfDay1 = localDate.atTime(LocalTime.MIN);
//or
LocalDateTime startOfDay2 = LocalTime.MIN.atDate(localDate);

//Current zone
ZonedDateTime startOfDayInZone = localDate.atTime(LocalTime.MIN)
        .atZone(ZoneId.systemDefault());

//Specific Zone
ZonedDateTime startOfDayInEurope = localDate.atTime(LocalTime.MIN)
    	.atZone(ZoneId.of("Europe/Paris"));

3. Getting the End of Day

Similar to the start of the day, we can use the LocalTime.MAX constant to find the end of the date using the following techniques:

LocalDate localDate = LocalDate.now();

//Local date time
LocalDateTime endOfDay = localDate.atTime(LocalTime.MAX);
LocalDateTime endOfDay1 = LocalTime.MAX.atDate(localDate);

//Current zone
ZonedDateTime endOfDayInZone = localDate.atTime(LocalTime.MAX)
      .atZone(ZoneId.systemDefault());

//Specific Zone
ZonedDateTime endOfDayInEurope = localDate.atTime(LocalTime.MAX)
      .atZone(ZoneId.of("Europe/Paris"));

4. Conclusion

In this short tutorial, we learned to get the start of a day and the end of a given day. This information is helpful in cases where we have to filter all events that occurred in a single day, and other such timestamp comparisons.

Happy Learning !!

Sourcecode Download

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode