tl;dr
Duration.between(
ZonedDateTime.of(
LocalDate.parse( "26/02/2011" , DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) ) ,
LocalTime.parse( "11:00 AM" , DateTimeFormatter.ofPattern( "hh:mm a" ) ) ,
ZoneId.of( "America/Montreal" )
)
,
ZonedDateTime.of(
LocalDate.parse( "27/02/2011" , DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) ) ,
LocalTime.parse( "12:15 AM" , DateTimeFormatter.ofPattern( "hh:mm a" ) ) ,
ZoneId.of( "America/Montreal" )
)
).toString()
See live code in IdeOne.com.
Time zone
The Question and the other Answers all ignore the crucial issue of time zone. You cannot calculate elapsed time between two date-time strings without knowing the intended time zone. For example, in places with Daylight Saving Time (DST), on the night of the cut-over, a day may be 23 hours long or 25 hours long rather than 24 hours long.
java.time
The modern way to do date-time work is with the java.time classes. These supplant the troublesome old date-time classes such as java.util.Date
and java.util.Calendar
.
Local…
First parse the input strings. These lack any indication of time zone, so we parse them as Local…
types.
Define a DateTimeFormatter
to match your string inputs. By the way, in the future, use standard ISO 8601 formats when serializing date-time values to text.
DateTimeFormatter df = DateTimeFormatter.ofPattern( "dd/MM/uuuu" );
LocalDate ld = LocalDate.parse( "26/02/2011" , df ) ;
ld.toString(): 2011-02-2011
DateTimeFormatter tf = DateTimeFormatter.ofPattern( "hh:mm a" );
LocalTime lt = LocalTime.parse( "11:00 AM" , tf ) ;
lt.toString(): 11:00:00
ZoneId
You need to know the time zone intended by your business scenario. I will arbitrarily choose one.
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
Apply the zone to get a ZonedDateTime
.
ZonedDateTime zdtStart = ZonedDateTime.of( ld , lt , z );
zdtStart.toString(): 2011-02-26T11:00:00-05:00[America/Montreal]
Duration
Do the same to get a zdtStop
. Calculate the elapsed time as a span of time not attached to the timeline, in a Duration
.
Duration d = Duration.between( zdtStart , zdtStop );
Call toString
to generate a String in standard ISO 8601 format for durations: PnYnMnDTnHnMnS
. The P
marks the beginning while the T
separates the two portions.
String output = d.toString();
d.toString(): PT13H15M
In Java 9 and later, call the to…Part
methods to access each component.
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?
- Java SE 8 and SE 9 and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
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.