24 hours != 1 day
You ignored the comments by Jon Skeet. You must become conciously aware of the fact that one day does not mean 24 hours. Because of anomalies such as Daylight Saving Time (DST), a day can run longer or shorter than 24 hours.
You need to be clear about which period your situation requires, 24 hours or one day.
Time Zone
For adding 24 hours, time zone is irrelevant.
Time zone is crucial in determining dates and days. For any given moment, the date varies around the world. For example a new day dawns earlier in Paris than in Montréal.
Furthermore, time zone defines rules for handling anomalies such as Daylight Time Saving (DST).
When omitted, your JVM’s current default time zone is applied. This is risky as that default can change at any moment during runtime by any code in any thread of any app in that JVM calling TimeZone.setDefault
.
I suggest you always specify a time zone by passing the optional argument rather than rely implicitly on the JVM’s current default. Ditto for Locale
.
java.time supplants Joda-Time
The makers of Joda-Time went on to define JSR 310, now implemented as the java.time framework built into Java 8 and later. While Joda-Time is still actively supported, the makers have asked us to move to java.time as soon as is convenient.
Convert your java.util.Date
to an Instant
, a moment on the timeline in UTC.
Instant instant = myJavaUtilDate.toInstant();
Add 24 hours
Adding 24 hour is simple.
Instant instantPlus24Hours = instant.plus( 24 , ChronoUnit.Hours );
Add 1 day
Adding a day requires the context of a particular time zone. An Instant
is always in UTC by definition. So we need to apply a time zone to generate a ZonedDateTime
.
ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.ofInstant( instant , zoneId );
ZonedDateTime zdtDayLater = zdt.plusDays( 1 );