我希望使用Joda Time获得两次P(开始时间)和Q(结束时间)之间的差异.P和Q可以是不同日期甚至同一天的时间.我希望得到格式HH-MM-SS的差异,其中H =小时,M =分钟,S =秒.
我想在计时器中使用此功能.我假设没有人会使用我的计时器测量超过24小时.
请指导我这样做.
Ice*_*Man 19
看看Joda time FAQ http://joda-time.sourceforge.net/faq.html#datediff
您可以使用a PeriodFormatter来获取您选择的格式.请尝试以下示例代码.
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));
Run Code Online (Sandbox Code Playgroud)