自动人类可读的时间间隔 [英] Automatic human readable time interval
问题描述
我想以人类可读的格式表达一些时间间隔.我正在寻找一个能够自动选择正确大小的库.间隔范围从毫秒到数周.类似的东西
I would like to express some time intervals in a human readable format. I was looking for a library automatically able to choose the correct size of the units. The intervals will range from milliseconds to weeks. Something like
- 0.2s
- 2:00
- 4 天
- ...
我知道我可以手动检查参数的大小,然后将其转换,但在重新发明轮子之前,我想问一下是否有(标准)库可以实现这一点.
I know I could manually check the size of the argument and then convert it but before reinventing the wheel I wanted to ask if there is a (standard) library to achieve that.
- TimeUnit 进行转换但不知道参数的大小
- a Joda-Time Duration 正是我需要的,但我没有找到太多关于表示的内容(但是对于 ReadableDuration)
- TimeUnit does the conversions but there is no knowledge about the size of the argument
- a Joda-Time Duration represents exactly what I need but I did not find much about representation (but for the ISO8601 formatting of ReadableDuration)
编辑:
我的输入(以纳秒为单位)和如上所述的输出.
My input in in nanoseconds and output as described above.
推荐答案
检查 PrettyTime
PrettyTime 是一个开源时间格式库.完全可定制,PrettyTime 创建了人类可读的相对时间戳,就像在 Digg、Twitter 和 Facebook 上看到的那样.很简单,立即开始"!并且支持超过 18 种语言!
PrettyTime is an OpenSource time formatting library. Completely customizable, PrettyTime creates human readable, relative timestamps like those seen on Digg, Twitter, and Facebook. It’s simple, get started "right now!" and in over 18 languages!
例如
PrettyTime p = new PrettyTime();
System.out.println(p.format(new Date()));
//prints: "right now"
PrettyTime t = new PrettyTime(new Date(3155692597470L * 3L));
t.format(new Date(0));
//3 centuries ago
这篇关于自动人类可读的时间间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!