简体   繁体   English

计时器 - 如何使用Joda Time计算两个日期之间的差异?

[英]Timer - How do I calculate the difference between two dates using Joda Time?

I want to get the difference between two times P (start time) and Q (end time) using Joda Time. 我希望使用Joda Time获得两次P(开始时间)和Q(结束时间)之间的差异。 P and Q could be times on different days or even on the same day. P和Q可以是不同日期甚至同一天的时间。 I want to get the difference in format HH-MM-SS, where H=hours, M=minutes, S=seconds. 我希望得到格式HH-MM-SS的差异,其中H =小时,M =分钟,S =秒。

I want to use this functionality in a timer. 我想在计时器中使用此功能。 I assume that no one will use my timer to measure more than 24 hours. 我假设没有人会使用我的计时器测量超过24小时。

Please guide me to do this. 请指导我这样做。

Take a look at the Joda time FAQ http://joda-time.sourceforge.net/faq.html#datediff 看看Joda time FAQ http://joda-time.sourceforge.net/faq.html#datediff

And you can use a PeriodFormatter to get the format of your choice. 您可以使用PeriodFormatter来获取您选择的格式。 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));

I admire the Joda date/time API. 我很欣赏Joda的日期/时间API。 It offers some cool functionality and if I needed an immutable calendar or some of the esoteric calendar options it offers, I'd be all over it. 它提供了一些很酷的功能,如果我需要一个不可变的日历或它提供的一些深奥的日历选项,我会全身心投入。

It is still an external API though. 它仍然是一个外部API。

So ... why use it when you don't have to. 所以...为什么在你不需要的时候使用它。 In the Joda API, the "Instant" is the exact same thing as a Java API "Date" (or pretty close to it). 在Joda API中,“Instant”与Java API“Date”(或非常接近它)完全相同。 These are both thin wrappers around a long that represents an instant in POSIX EPOCH UTC time (which is the number of milliseconds that have elapsed since 00:00am Jan 1, 1970 UTC. 这些都是围绕长度的薄包装,代表POSIX EPOCH UTC时间的瞬间(这是自UTC时间1970年1月1日00:00以来经过的毫秒数。

If you have either two Instants or two Dates, computing the days between them is trivial, and the Joda library is absolutely not needed for this purpose alone: 如果您有两个Instants或两个Dates,那么计算它们之间的天数是微不足道的,并且仅为此目的绝对不需要Joda库:

public double computeDaysBetweenDates(Date earlier, Date later) {
    long diff;

    diff = later.getTime() - earlier.getTime();
    return ((double) diff) / (86400.0 * 1000.0);
}

This assumes that the number of seconds in a day is 86400 ... and this is mostly true. 这假设一天中的秒数是86400 ......这大部分都是正确的。

Once you have a difference as a double, it is trivial to convert the fractional portion of the answer (which is the fraction of one day) into HH:MM:SS. 一旦你有一个双倍的差异,将答案的小数部分(这是一天的分数)转换为HH:MM:SS是微不足道的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM