Joda-Time
In Joda-Time, use the Minutes class. Basically one line of code, calling minutesBetween
.
Example Code
Here is some example code in Joda-Time 2.3.
Parsing those strings
DateTimeZone timeZone = DateTimeZone.forID( "Europe/Paris" );
DateTimeFormatter formatter = DateTimeFormat.forPattern( "MM/dd/yyyy HH:mm:ss" ).withZone( timeZone );
DateTime start = formatter.parseDateTime( "03/25/2014 18:03:00" );
DateTime stop = formatter.parseDateTime( "03/25/2014 19:45:00" );
Calculating minutes between
int minutes = Minutes.minutesBetween( start, stop ).getMinutes();
ISO Duration (Period)
Or you may want to generate a string in the ISO 8601 format of Durations: PnYnMnDTnHnMnS
. To do so in Joda-Time, use the Period class (yes, date-time terminology is not standardized, used differently by different folks).
Period period = new Period( start, stop );
String output = period.toString();
Results
Dump to console…
System.out.println( "start: " + start );
System.out.println( "stop: " + stop );
System.out.println( "minutes: " + minutes );
System.out.println( "period: " + period );
When run…
start: 2014-03-25T18:03:00.000+01:00
stop: 2014-03-25T19:45:00.000+01:00
minutes: 102
period: PT1H42M
That output of PT1H42M
means "one hour and forty-two minutes".
Time Zone
Your question and code ignored the crucial issue of time zone in parsing those strings. You should almost always specify a time zone.
java.time
The new java.time package in Java 8 may have similar features as what you’ve seen here with Joda-Time.