Explain Codes LogoExplain Codes Logo

How to add one day to a date?

java
date-manipulation
java-8-api
time-zones
Nikita BarsukovbyNikita Barsukov·Jan 14, 2025
TLDR

Quickly increment any Date object by one day using Java's Calendar API:

Calendar calendar = Calendar.getInstance(); calendar.setTime(date); // don't forget to invite your date calendar.add(Calendar.DATE, 1); // asks the Calendar for a second date, but tomorrow Date tomorrow = calendar.getTime(); // got stood up? Time to move on!

Replace date with your specific Date object to yield tomorrow, symbolizing the next day.

Empowering date manipulation with Java 8

The JSR 310 API (Java 8 Time API) opened an intuitive world for date manipulation. Use classes like LocalDate, LocalDateTime, and ZonedDateTime to easily add days to dates:

LocalDate today = LocalDate.now(); // just a (sun)day in the park LocalDate tomorrow = today.plusDays(1); // Tomorrowland is just a day away!

Align ZonedDateTime to manage time zones and daylight saving time:

ZonedDateTime zonedNow = ZonedDateTime.now(); // Now you're in the zone! ZonedDateTime zonedTomorrow = zonedNow.plusDays(1); // sky-zoned into tomorrow

For UTC or epoch milliseconds adjustments, pair Instant with Duration:

Instant now = Instant.now(); // snap! you're a time traveler Instant tomorrow = now.plus(1, ChronoUnit.DAYS); // next stop: future!

Legacy date wrangling

In environments yet to adopt Java 8 or higher, the Calendar class is your best friend (with benefits?). Beware though, java.util.Date has limitations:

long currentTimeMillis = date.getTime(); Date tomorrow = new Date(currentTimeMillis + 86400000L); // disclaimer: flux capacitor not included!

But watch out, adding milliseconds directly may tickle daylight saving time and time zones in funny ways.

Keep time zones in check

Global citizen? Your app probably is too, so grappling time zones is crucial. Let java.time take the wheel:

ZoneId zoneId = ZoneId.of("America/Los_Angeles"); // West coast, best coast? LocalDate today = LocalDate.now(zoneId); // time after time(zone) LocalDate tomorrow = today.plusDays(1); // always day ahead

Ditch the cryptic short codes ('EST', 'PDT'). Go with longform ZoneId identifiers.

Humanizing date arithmetic

Narrate time spans conveniently using Period and Duration:

Period oneDay = Period.ofDays(1); // One day more, another day, another destiny (Les Mis, anyone?) LocalDate tomorrow = LocalDate.now().plus(oneDay); // Add a day, keep the doctor away

For Instant or time-specific objects, use durations.

Database daydreams

Marriage of java.time and databases isn't a distant dream. Say "I do" to JDBC driver compliant with JDBC 4.2 or later for native java.time support.

Dodge those exceptions

Handle potential DateTimeException elegantly. Remember, February 29th can be a tricky date to mess with, leap-year or not.

Busting first moment misunderstandings

Rely on java.time framework for real first moment of the day without assuming it's 00:00:00—not all days start at midnight!

LocalDate today = LocalDate.now(); ZonedDateTime beginningOfDay = today.atStartOfDay(ZoneId.systemDefault()); // midnight isn't always the witching hour

Using Joda-Time

Prior to Java 8, Joda-Time was the hotshot. It’s still a viable date-time API for systems still cruising on older Java versions:

org.joda.time.LocalDate jodaToday = new org.joda.time.LocalDate(); // Today's date, old-school style org.joda.time.LocalDate jodaTomorrow = jodaToday.plusDays(1); // Getting tomorrow's newspaper today!

Note that Joda-Time maintenance is slowing down in the era of Java 8’s java.time.