tl;dr
ZoneId z = ZoneId.of( "Pacific/Auckland" ) ;
ZonedDateTime now = ZonedDateTime.now( z ) ;
Duration d = Duration.between(
now ,
ZonedDateTime.of(
now.toLocalDate().plusDays(1) ,
LocalTime.of( 2 , 0 )
z
) ;
).toString()
PT8H32M57.264S
Time-Zone
Other answers are correct except for one big flaw: time zone. All the code shown defaults to the JVM's current default time zone. When deploying code to another machine, you could have unexpected behavior. Daylight Saving Time (DST) or other anomalies tied to a time zone could alter the calculation.
java.time
The modern approach uses the java.time classes.
A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.
Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 3-4 letter abbreviation such as EST
or IST
as they are not true time zones, not standardized, and not even unique(!).
ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime now = ZonedDateTime.now( z );
We need to build the target date-time, tomorrow at 2 AM. The tomorrow's date as a LocalDate
(date-only value) plus the target time as a LocalTime
(time-of-day only value). The java.time classes use 24-hour clock by default, so we needn't worry about your 2 AM vs 2 PM problem. A 2 PM values would have an input of 14 hours, so no ambiguity.
LocalDate tomorrow = now.toLocalDate().plusDays( 1 ) ;
LocalTime targetTime = LocalTime.of( 2 , 0 ) ; // 2 hours, zero minutes.
Caveat: If you happened to pick a particular date and time that is invalid, such as a Daylight Saving Time (DST) cutover, the ZonedDateTime
class adjusts to fit. Read the class documentation to see if its algorithms for adjusting fit your needs/expectations. For example, on the date of the DST "Spring-forward" date, you would end up with 3 AM rather than 2 AM as the 02:00 hour does not exist on that date, only 01:00 & 03:00 hours exist. This will not affect your calculation of a duration. The space-time continuum did not jump, just our clock-on-the-wall representation jumped.
Put them together with the time zone to get 2 AM tomorrow.
ZonedDateTime target = ZonedDateTime.of( tomorrow , targetTime , z ) ;
Our ultimate goal it to capture the elapsed time between now and that target tomorrow. To represent a span-of-time unattached to the timeline, use the Duration
or Period
classes.
Duration d = Duration.between( now , target ) ;
Call toString
to generate a String formatted according to the ISO 8601 standard, PnYnMnDTnHnMnS
. The P
marks the beginning, the T
separates the years-months-days portion from the hours-minutes-seconds portion.
PT8H32M57.264S
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
Joda-Time
UPDATE: The Joda-Time project is now in maintenance mode, with the team advising migration to the java.time classes. See Tutorial by Oracle. This section here left intact for history.
Below is some example code using the Joda-Time 2.3 library.
The DateTime
class knows its own time zone.
The method withTime
creates a new (immutable) instance copied from the existing instance, but with the specified time of day.
Joda-Time has a few classes to represent a span of time: Period, Duration, Interval. The Period class by defaults renders a string based on the ISO 8601 Duration format of PnYnMnDTnHnMnS
. You asked for milliseconds, so I calculate that in the example. See this question, Joda time, Period to total millis, for more detail.
Example Code
// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;
// Better to specify a time zone explicitly rather than rely on default.
// Time Zone list… http://joda-time.sourceforge.net/timezones.html (not quite up-to-date, read page for details)
DateTimeZone timeZone = DateTimeZone.forID( "Europe/Paris" );
DateTime now = new DateTime( timeZone );
DateTime tomorrowTwoInMorning = now.plusDays( 1 ).withTime( 2, 0, 0, 0 );
// Calculate the span of time between them.
Period period = new Period( now, tomorrowTwoInMorning );
long milliseconds = period.toStandardDuration().getMillis();
Dump to console…
System.out.println( "now: " + now );
System.out.println( "tomorrowTwoInMorning: " + tomorrowTwoInMorning );
System.out.println( "period: " + period );
System.out.println( "milliseconds: " + milliseconds );
When run…
now: 2014-01-11T00:51:38.059+01:00
tomorrowTwoInMorning: 2014-01-12T02:00:00.000+01:00
period: P1DT1H8M21.941S
milliseconds: 90501941