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 at IdeOne.com .
Timezone
The question and other answers ignore the crucial time zone problem. You cannot calculate the elapsed time between two date strings without knowing the estimated time zone. For example, in places with daylight saving time (DST), at night cutting time, the day can be 23 hours or 25 hours, not 24 hours.
java.time
The modern way to do work with date is with the java.time classes. They supersede unpleasant old time classes such as java.util.Date and java.util.Calendar .
Local…
Parse the input strings first. They do not have any indication of the time zone, so we analyze them as Local… types Local…
Define a DateTimeFormatter to match string inputs. By the way, in the future, use standard ISO 8601 formats when serializing date and time values in 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 designed for your business scenario. I will arbitrarily choose it.
Specify the time zone name in continent/region format, such as America/Montreal , Africa/Casablanca or Pacific/Auckland . Never use an abbreviation of 3-4 characters, for example EST or IST , as they are not real time zones, and are not standardized or even unique (!).
ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime
Apply the zone to get the 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 zdtStop . Calculate elapsed time as a time span that is not tied to a timeline in Duration .
Duration d = Duration.between( zdtStart , zdtStop );
Call toString to generate a string in the standard ISO 8601 format for the duration : PnYnMnDTnHnMnS . P marks the beginning, while T divides the two parts.
String output = d.toString();
d.toString (): PT13H15M
In Java 9 and later, call the to…Part methods to…Part access each component.
About java.time
The java.time framework is built into Java 8 and later. These classes supersede the nasty old legacy time classes such as java.util.Date , Calendar and SimpleDateFormat .
The Joda-Time project, now in maintenance mode , we recommend switching to the java.time classes.
To learn more, see the Oracle Tutorial . And search for qaru for many examples and explanations. JSR 310 specification .
Where to get java.time classes?
- Java SE 8 and SE 9 and later
- Built in.
- Part of the standard Java API with integrated implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and SE 7
- Most of the functionality of java.time is ported back to Java 6 and 7 in ThreeTen-Backport .
- Android
- The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) specifically for Android.
- See How to use ....
The ThreeTen-Extra project extends java.time with additional classes. This project is a proof of possible future additions to java.time. Here you can find useful classes such as Interval , YearWeek , YearQuarter and more .