Calculate Difference Between Two Dates – Java Example
Take a look @ Joda-Time FAQ
You can use a PeriodFormatter to get the format of your choice.
Try the following sample code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
DateTime dt = new DateTime(); DateTime twoHoursLater = dt.plusHours(2).plusMinutes(10).plusSeconds(5); Period period = new Period(dt, twoHoursLater); PeriodFormatter HHMMSSFormater = new PeriodFormatterBuilder() .printZeroAlways() .minimumPrintedDigits(2) .appendHours().appendSeparator("-") .appendMinutes().appendSeparator("-") .appendSeconds(); .toFormatter(); // produce thread-safe formatter System.out.println(HHMMSSFormater.print(period)); |