Get Elapsed Time since Midnight in Java

Calculating the elapsed time since midnight (in seconds or milliseconds) is a common task in various applications. This Java Date-Time tutorial demonstrates how to calculate the time passed since the clock struck midnight.

1. Get Elapsed Time Since Midnight

To get the elapsed time since midnight, we capture the current time using LocalTime.now() and midnight time LocalTime.MIDNIGHT and then calculate the difference between them by subtraction.

LocalTime midnight = LocalTime.MIDNIGHT;
LocalTime currentTime = LocalTime.now();

long seconds = ChronoUnit.SECONDS.between(midnight, currentTime);

The ChronoUnit supports different units of time (e.g. milliseconds) that we can utilize for obtaining the difference in other time units as well.

LocalTime midnight = LocalTime.MIDNIGHT;
LocalTime currentTime = LocalTime.now();

long millis = ChronoUnit.MILLIS.between(midnight, currentTime);

2. Get Elapsed Time Since Midnight in Specified Timezone

By default, LocalTime.now() returns the time in the system clock in the default/current timezone. If we wish to get the elapsed time in a specified time zone, we can pass the zone information as follows:

LocalTime currentTime = LocalTime.now(ZoneId.of("America/New_York"));
LocalTime midnight = LocalTime.MIDNIGHT;

long seconds = ChronoUnit.SECONDS.between(midnight, currentTime);

Similarly, we can get the elapsed time since midnight in UTC as follows:

LocalTime currentTime = LocalTime.now(ZoneOffset.UTC);
LocalTime midnight = LocalTime.MIDNIGHT;

long seconds = ChronoUnit.SECONDS.between(midnight, currentTime);

3. Using LocalDateTime or ZonedDateTime

If we have a LocalDateTime instance in the application, and we wish to utilize it then the process to get the elapsed time remains the same. We get the exact instance at midnight and then subtract the current time.

The following Java program utilizes the LocalDateTime class to obtain the elapsed time since midnight.

LocalDateTime currentTime = LocalDateTime.now();
LocalDateTime midnight = LocalDateTime.of(LocalDate.now(), LocalTime.MIDNIGHT);

long seconds = ChronoUnit.SECONDS.between(midnight, currentTime);

Similarly, we can use the process for the ZonedDateTime class as well.

ZonedDateTime currentTime = ZonedDateTime.now();
ZonedDateTime midnight = ZonedDateTime.of(LocalDate.now(), LocalTime.MIDNIGHT, ZoneId.systemDefault());

long seconds = ChronoUnit.SECONDS.between(midnight, currentTime);

4. Conclusion

In this Java tutorial, we learned the concept of elapsed time and used Java’s java.time package to calculate the time that has passed since midnight. We demonstrated to use of classes such as LocalTime, LocalDateTime, and ZonedDateTime in the Java programs.

Happy Learning !!

Source Code on Github

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