java java计算两个时间戳之间的时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6177830/
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
java calculate time between two timestamps
提问by DArkO
I need to calculate the time passed between two dates.
我需要计算两个日期之间经过的时间。
The catch here is that I need to show it as YouTube does with its video comments timestamps. That is, to show it by just the largest measure.
这里的问题是我需要像 YouTube 那样显示它的视频评论时间戳。也就是说,仅通过最大的度量来显示它。
For example,
例如,
- if the time is 50 seconds ago it should say 50 seconds ago.
- if the time is more than one minute it should say one minute ago/ten minutes ago etc..
- if the time difference is 1 hour 30 mins it should show: an hour ago.
- if the time is one and a half week than it should say one week ago.
- if the time is more than a month it should say one month ago/two months ago etc...
- and so on and so on..
- 如果时间是 50 秒前,它应该说 50 秒前。
- 如果时间超过一分钟,它应该说一分钟前/十分钟前等等。
- 如果时差为 1 小时 30 分钟,则应显示:一小时前。
- 如果时间是一周半而不是一周前。
- 如果时间超过一个月,应该说一个月前/两个月前等等......
- 等等等等..
So what is the best way to handle this?
Should I make a method with case
or if
statements that would return something like this? Or is there a better approach (maybe a library which already does something like it)?
那么处理这个问题的最佳方法是什么?我应该使用case
或if
语句创建一个方法来返回这样的内容吗?或者有没有更好的方法(也许一个已经做过类似事情的图书馆)?
回答by user775598
Use DateUtils.getRelativeTimeSpanString(long time, long now, long minResolution). time
is the start time, and now
is the end time (in milliseconds). To report "seconds ago," set minResolution to zero.
使用DateUtils.getRelativeTimeSpanString(long time, long now, long minResolution)。time
是开始时间,now
是结束时间(以毫秒为单位)。要报告“几秒前”,请将 minResolution 设置为零。
Example:
例子:
String result = DateUtils.getRelativeTimeSpanString(1306767830, 1306767835, 0);
// result = "5 seconds ago"
回答by RMT
date1.getTime() - date2.getTime()
This will return you the time in miliseconds between the 2 dates. Just convert that to what ever you want to show (e.g. hours minutes seconds)
这将返回两个日期之间的时间(以毫秒为单位)。只需将其转换为您想要显示的内容(例如小时分秒)
回答by Femi
On Android, use this: http://developer.android.com/reference/android/text/format/DateUtils.html#getRelativeTimeSpanString%28android.content.Context,%20long,%20boolean%29
在 Android 上,使用这个:http: //developer.android.com/reference/android/text/format/DateUtils.html#getRelativeTimeSpanString%28android.content.Context,%20long,%20boolean%29
回答by P?l Brattberg
Take a look at PrettyTime!
看看PrettyTime!
Also, everytime you want to do something date/time-related in Java, you should take a look at Joda Time. Do it now, you will thank me later.
此外,每次你想在 Java 中做一些与日期/时间相关的事情时,你应该看看Joda Time。现在就去做,以后你会感谢我的。
回答by solendil
Your need is very specific, and I don't know any lib that would solve the problem for you out of the box. However the problem is not very complex and a small function full of "ifs" should do the trick. Of course, a nice date library like Joda Time will help you keep your code clean. Who wants to use GregorianCalendar!?
您的需求非常具体,我不知道任何可以立即为您解决问题的库。然而,问题并不是很复杂,一个充满“ifs”的小函数应该可以解决问题。当然,像 Joda Time 这样不错的日期库将帮助您保持代码整洁。谁想使用公历!?
回答by Andreas Dolk
Looks like you have a set of custom rules and the algorithm to choose a rule is based on the time in seconds between two timestamps. The easiest approach is to handle the rulesin a series of if/else if statements:
看起来您有一组自定义规则,并且选择规则的算法基于两个时间戳之间的时间(以秒为单位)。最简单的方法是在一系列 if/else if 语句中处理规则:
private String getTimeAsString(int seconds) {
if (seconds < 60) { // rule 1
return String.format("%s seconds ago", seconds);
} else if (seconds < 3600) { // rule 2
return String.format("%s minutes ago", seconds/60);
} // ... and so on
}