tl;dr
For Java 6 and Java 7, use the ThreeTen-Backport project.
12 hours
Between 2 PM and 2 AM in Europe/Berlin
time zone on those dates is twelve hours.
org.threeten.bp.temporal.ChronoUnit.HOURS.between(
org.threeten.bp.LocalDateTime
.parse( "2017-10-28T14:00:00" )
.atZone( org.threeten.bp.ZoneId.of( "Europe/Berlin" ) ) , // Producing a `ZonedDateTime` instance.
org.threeten.bp.LocalDateTime
.parse( "2017-10-29T02:00:00" )
.atZone( org.threeten.bp.ZoneId.of( "Europe/Berlin" ) ) // Producing another `ZonedDateTime` instance.
)
12
13 hours
To see the effect of a Daylight Saving Time (DST) cutover scheduled for 3 AM in Berlin time, change your start-stop to 3 PM and 3 AM, to get 13 hours rather than 12 hours.
The hour of 02:00-03:00 repeats, as discussed in the Answer by Hugo.
org.threeten.bp.temporal.ChronoUnit.HOURS.between(
org.threeten.bp.LocalDateTime
.parse( "2017-10-28T15:00:00" )
.atZone( org.threeten.bp.ZoneId.of( "Europe/Berlin" ) ) , // Producing a `ZonedDateTime` instance.
org.threeten.bp.LocalDateTime
.parse( "2017-10-29T03:00:00" )
.atZone( org.threeten.bp.ZoneId.of( "Europe/Berlin" ) ) // Producing another `ZonedDateTime` instance.
)
13
See this code run live at IdeOne.com, using the java.time classes built into Java 8.
java.time
This has been covered many many times already on Stack Overflow, so search for more info.
Briefly…
Avoid the Date
class. Use only the java.time classes. Likewise, avoid the Joda-Time project for new work as the team advises migration to java.time classes.
Parse your input strings as LocalDateTime
values because they lack an indicator of time zone or offset-from-UTC. A LocalDateTime
does not represent an actual moment on the timeline, but we'll get there; read on.
Tip: write your date-time strings in standard ISO 8601 format, used by default in java.time classes when parsing/generating strings.
Define a time zone (ZoneId
) intended for these values. Specify a zone by proper name in continent/region
format such as Africa/Casablanca
or Europe/Berlin
. Never use 3-4 pseudo zone names such as PST
or CST
.
Apply the zone to produce a ZonedDateTime
object for each moment.
Calculate elapsed time instantiating a Duration
object, or by calling ChronoUnit.HOURS.between
.
Much of java.time functionality is back-ported to Java 6 & Java 7 in the ThreeTen-Backport project. The java.time classes are built into Java 8 and Java 9.

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?