Java 如何使用 Joda-Time 计算从现在起经过的时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2179644/
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-08-13 04:28:03  来源:igfitidea点击:

How to calculate elapsed time from now with Joda-Time?

javaalgorithmdatetimegrailsjodatime

提问by fabien7474

I need to calculate the time elapsed from one specific date till nowand display it with the same format as StackOverflow questions, i.e.:

我需要计算从一个特定日期到现在所经过时间,并以与 StackOverflow 问题相同的格式显示它,即:

15s ago
2min ago
2hours ago
2days ago
25th Dec 08

Do you know how to achieve it with the Java Joda-Timelibrary? Is there a helper method out there that already implements it, or should I write the algorithm myself?

您知道如何使用 Java Joda-Time实现它吗?是否有已经实现它的辅助方法,还是我应该自己编写算法?

采纳答案by BalusC

To calculate the elapsed time with JodaTime, use Period. To format the elapsed time in the desired human representation, use PeriodFormatterwhich you can build by PeriodFormatterBuilder.

要使用 JodaTime 计算经过的时间,请使用Period. 要在所需的人类表示中格式化经过的时间,请使用PeriodFormatter您可以构建的PeriodFormatterBuilder.

Here's a kickoff example:

这是一个启动示例:

DateTime myBirthDate = new DateTime(1978, 3, 26, 12, 35, 0, 0);
DateTime now = new DateTime();
Period period = new Period(myBirthDate, now);

PeriodFormatter formatter = new PeriodFormatterBuilder()
    .appendSeconds().appendSuffix(" seconds ago\n")
    .appendMinutes().appendSuffix(" minutes ago\n")
    .appendHours().appendSuffix(" hours ago\n")
    .appendDays().appendSuffix(" days ago\n")
    .appendWeeks().appendSuffix(" weeks ago\n")
    .appendMonths().appendSuffix(" months ago\n")
    .appendYears().appendSuffix(" years ago\n")
    .printZeroNever()
    .toFormatter();

String elapsed = formatter.print(period);
System.out.println(elapsed);

This prints by now

这现在打印

3 seconds ago
51 minutes ago
7 hours ago
6 days ago
10 months ago
31 years ago

(Cough, old, cough)You see that I've taken months and years into account as well and configured it to omit the values when those are zero.

(咳,老,咳)你看,我也考虑了几个月和几年,并将其配置为在这些值为零时省略这些值。

回答by sfussenegger

There is a small helper class called HumanTimethat I'm pretty happy with.

有一个叫做HumanTime的小助手类,我很满意。

回答by snappieT

You can do this with a PeriodFormatter but you don't have to go to the effort of making your own PeriodFormatBuilder as in other answers. If it suits your case, you can just use the default formatter:

您可以使用 PeriodFormatter 来做到这一点,但您不必像其他答案一样努力制作自己的 PeriodFormatBuilder 。如果它适合你的情况,你可以使用默认的格式化程序:

Period period = new Period(startDate, endDate);
System.out.println(PeriodFormat.getDefault().print(period))

(hat tip to this answeron a similar question, I'm cross-posting for discoverability)

(对类似问题的这个答案的提示,我交叉发布以提高可发现性)

回答by Joshua Pinter

Use PrettyTimefor Simple Elapsed Time.

使用PrettyTime表示简单的经过时间。

I tried HumanTimeas @sfussenegger answered and using JodaTime's Periodbut the easiest and cleanest method for human readable elapsed time that I found was the PrettyTimelibrary.

我在@sfussenegger 回答并使用 JodaTime 时尝试了HumanTimePeriod但我发现的人类可读经过时间的最简单和最干净的方法是PrettyTime库。

Here's a couple of simple examples with input and output:

下面是几个带有输入和输出的简单示例:

Five Minutes Ago

五分钟前

DateTime fiveMinutesAgo = DateTime.now().minusMinutes( 5 );

new PrettyTime().format( fiveMinutesAgo.toDate() );

// Outputs: "5 minutes ago"

Awhile Ago

不久以前

DateTime birthday = new DateTime(1978, 3, 26, 12, 35, 0, 0);

new PrettyTime().format( birthday.toDate() );

// Outputs: "4 decades ago"

CAUTION:I've tried playing around with the library's more precise functionality, but it produces some odd results so use it with care and in non-life threatening projects.

注意:我曾尝试使用库的更精确的功能,但它会产生一些奇怪的结果,因此请谨慎使用它并在非危及生命的项目中使用它。

JP

J.P