Explain Codes LogoExplain Codes Logo

How to create a Java Date object of midnight today and midnight tomorrow?

java
time-zones
java-8
date-and-time
Alex KataevbyAlex Kataev·Nov 23, 2024
TLDR
LocalDateTime midnightToday = LocalDate.now().atStartOfDay(); LocalDateTime midnightTomorrow = midnightToday.plusDays(1);

Make use of the streamlined Java 8 LocalDate.now() functionality to get the current date and employ atStartOfDay() to derive midnight today. Then, for midnight tomorrow, simply use plusDays(1) with midnightToday. Quick, clean, and timezone-observant.

Handling Time Zones

Keep in mind time zones when working with timestamps. It's essential they align correctly with your specific locale:

ZoneId myZone = ZoneId.of("America/New_York"); // Yay! NY pizza time 🍕 LocalDateTime midnightTodayAtMyZone = LocalDate.now(myZone).atStartOfDay(); LocalDateTime midnightTomorrowAtMyZone = midnightTodayAtMyZone.plusDays(1); // 🎉 Party is going to be longer today!

Working with Java 7 or earlier

If you're working on Java 7 or earlier, using Calendar might be more verbose but it's definitely still effective:

Calendar midnightTodayLegacy = Calendar.getInstance(); midnightTodayLegacy.set(Calendar.HOUR_OF_DAY, 0); midnightTodayLegacy.set(Calendar.MINUTE, 0); midnightTodayLegacy.set(Calendar.SECOND, 0); midnightTodayLegacy.set(Calendar.MILLISECOND, 0); Calendar midnightTomorrowLegacy = (Calendar) midnightTodayLegacy.clone(); midnightTomorrowLegacy.add(Calendar.DAY_OF_MONTH, 1); // One more day, just stick with me here!

Using Instant for UTC time

Working with Universal Coordinated Time (UTC) comes in handy for consistency across various time zones, which applies to global services. Instant in Java 8 was designed for this:

Instant midnightTodayUTC = Instant.now().truncatedTo(ChronoUnit.DAYS); Instant midnightTomorrowUTC = midnightTodayUTC.plus(1, ChronoUnit.DAYS); // I love time travel!

Simplification with Apache Commons Lang

The DateUtils from Apache Commons Lang can simplify this task, assuming it's part of your project:

Date midnightTodayCommons = DateUtils.truncate(new Date(), Calendar.DAY_OF_MONTH); Date midnightTomorrowCommons = DateUtils.addDays(midnightTodayCommons, 1);

This looks simpler and is absolutely more readable.

Beware of Precisions and Pitfalls!

Ensure you're wary of the Daylight Saving Time changes since they can cause unexpected results:

ZonedDateTime zonedMidnightToday = midnightTodayAtMyZone .toLocalDate() .atStartOfDay(myZone); ZonedDateTime zonedMidnightTomorrow = zonedMidnightToday.plusDays(1); // You didn't forget the DST, did you?

Say No to Deprecated Methods!

For Joda-Time users, avoid using DateMidnight as it is deprecated. Choose this instead:

DateTime midnightTodayJoda = new DateTime().withTimeAtStartOfDay(); DateTime midnightTomorrowJoda = midnightTodayJoda.plusDays(1);

Leap Seconds? Leap Years? Leap...Frog?

Rarely, additions like leap seconds or other time corrections can ambush your time calculations. Extreme precision required? Check for such anomalies and make sure your code can jump these hurdles.