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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12397413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:44:13  来源:igfitidea点击:

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

javajodatime

提问by david blaine

I want to get the difference between two times P (start time) and Q (end time) using Joda Time. P and Q could be times on different days or even on the same day. I want to get the difference in format HH-MM-SS, where H=hours, M=minutes, S=seconds.

我想使用 Joda Time 获得两次 P(开始时间)和 Q(结束时间)之间的差异。P 和 Q 可以是不同天甚至同一天的时间。我想获得格式 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.

请指导我这样做。

回答by IceMan

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

看看 Joda 时间常见问题 http://joda-time.sourceforge.net/faq.html#datediff

And you can use a PeriodFormatterto get the format of your choice. Try the following sample code.

您可以使用 aPeriodFormatter来获取您选择的格式。尝试以下示例代码。

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));

回答by scottb

I admire the Joda date/time 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.

我很欣赏 Joda 日期/时间 API。它提供了一些很酷的功能,如果我需要一个不可变的日历或它提供的一些深奥的日历选项,我会全力以赴。

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). 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.

所以......为什么在你不需要的时候使用它。在 Joda API 中,“即时”与 Java API“日期”(或非常接近)完全相同。它们都是围绕 long 的薄包装器,表示 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 就很简单了。