Explain Codes LogoExplain Codes Logo

How to obtain the start time and end time of a day?

java
time-management
date-handling
java-8
Anton ShumikhinbyAnton ShumikhinΒ·Dec 22, 2024
⚑TLDR

To easily get the start and end times of the present day, use java.time.LocalDate and java.time.LocalTime. Here is the quick code:

import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; LocalDateTime startOfDay = LocalDate.now().atStartOfDay(); LocalDateTime endOfDay = LocalDate.now().atTime(LocalTime.MAX); System.out.println("Start: " + startOfDay); // Prints when the rooster crows πŸ” System.out.println("End: " + endOfDay); // Prints right before Cinderella's carriage turns into a pumpkin πŸŽƒ

The function atStartOfDay gives you the start of the day (00:00), while LocalTime.MAX gives you the millisecond before the next day begins (23:59:59.999).

Handling edge cases and advanced tricks

Juggling multiple time zones

If you're managing time zones, it's worth pinpointing ZoneId. Check the code below:

import java.time.ZoneId; import java.time.ZonedDateTime; ZoneId zoneId = ZoneId.of("America/New_York"); // New Yorkers, rejoice! πŸ—½ ZonedDateTime zonedStartOfDay = LocalDate.now().atStartOfDay(zoneId); ZonedDateTime zonedEndOfDay = zonedStartOfDay.plusDays(1).minusNanos(1); System.out.println("Zoned Start: " + zonedStartOfDay); // NYC is waking up β˜• System.out.println("Zoned End: " + zonedEndOfDay); // NYC falls asleep πŸŒƒ

For those sailing in the Java 7 and earlier sea, use robust libraries like Apache Commons Lang, or stick to manual setting:

import org.apache.commons.lang3.time.DateUtils; import java.util.Calendar; Calendar start = Calendar.getInstance(); start.setTime(DateUtils.truncate(start.getTime(), Calendar.DAY_OF_MONTH)); // Day has just started! Calendar end = (Calendar) start.clone(); end.add(Calendar.DAY_OF_MONTH, 1); end.add(Calendar.MILLISECOND, -1); // You've got 1 ms to midnight! System.out.println("Legacy Start: " + start.getTime()); System.out.println("Legacy End: " + end.getTime());

To the millisecond and beyond!

For those applications craving for millisecond precision, here's how to quench their thirst:

LocalDateTime endOfDayMillis = LocalDate.now().atTime(23, 59, 59, 999_999_999); System.out.println("End with Milliseconds: " + endOfDayMillis); // It's milli milli exciting!

Maintaining consistency across all apps

For accurate and consistent time tracking, all applications should follow the same definitions:

  • Stick to LocalTime.MIN and LocalTime.MAX
  • Stick to standard zone offsets for global applications
  • Stick to using Instant for applications logged and timestamped across systems

Handling intervals like a boss

To ace your period management game, go with the Interval class:

import java.time.*; import java.time.temporal.ChronoUnit; Instant startInstant = LocalDate.now().atStartOfDay().toInstant(ZoneOffset.UTC); Instant endInstant = LocalDate.now().plusDays(1).atStartOfDay().minusNanos(1).toInstant(ZoneOffset.UTC); Duration dayDuration = Duration.between(startInstant, endInstant); // Oh, the times we've shared!

Checking if a date falls within a certain period

Determine if a specific date is within certain bounds as shown below:

boolean isWithinToday = !date.isBefore(startOfDay) && date.isBefore(endOfDay.plusSeconds(1)); // "Is it today yet?" -me, every Monday morning