Java – timer – how to calculate the difference between two dates using joda time?

I want to use joda time to get the difference between P (start time) and Q (end time) twice P and Q may be different days or even at the same time I want to get the difference of format hh-mm-ss, where h = hour, M = minute, s = second

I want to use this function in the timer I assume no one will use my timer to measure more than 24 hours

Please guide me to do so

Solution

Look at joda time FAQs

You can use periodformatter to get the format you choose Try the following sample code

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));
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>