Get current date. Note that a time zone is crucial here, as for any given moment the date varies around the globe by zone.
ZoneId z = ZoneId.of( "Asia/Tokyo" ) ;
ZonedDateTime now = ZonedDateTime.now( z ) ;
LocalDate today = now.toLocalDate() ;
Get the first moment of tomorrow. Do not assume the day starts at 00:00. Some dates in some zones start at another time. Let java.time determine the first moment.
ZonedDateTime startOfTomorrow = today.plusDays( 1 ).atStartOfDay( z ) ;
Calculate elapsed time.
Duration d = Duration.between( now , startOfTomorrow ) ;
Interrogate the duration for your desired number of whole seconds until tomorrow.
long secondsUntilTomorrow = d.toSeconds() ;
now
is aZonedDateTime
representing the current date and time in your time zone, you may get the remaining time of the day fromDuration.between(now, now.toLocalDate().plusDays(1).atStartOfDay(now.getZone()))
. You may search for how to format the resultingDuration
object in a nice and human-readable way. Or simply convert to seconds by calling itstoSeconds
method.now.plusDays(1)
you would additionally need.truncatedTo(ChronoUnit.DAYS)
. It would work, I think, but there are some corner cases around the day not always starting at 00:00:00 in all time zones that we’d need to think through.