java计算两个时间戳之间的时间
我需要计算两个日期之间经过的时间。
这里的问题是我需要像 YouTube 那样显示它的视频评论时间戳。也就是说,仅用最大的度量来显示它。
例如,
- 如果时间是 50 秒前,则应该说 50 秒前。
- 如果时间超过一分钟,则应显示一分钟前/十分钟前等。
- 如果时差为 1 小时 30 分钟,则应显示:一小时前。
- 如果时间是一周半,则应该是一周前。
- 如果时间超过一个月,则应该说一个月前/两个月前等等......等等
- ......
那么处理这个问题的最佳方法是什么? 我应该使用 case
或 if
语句创建一个返回类似内容的方法吗?或者是否有更好的方法(也许一个库已经做了类似的事情)?
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.
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..
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)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用 DateUtils.getRelativeTimeSpanString(很长一段时间,很长一段时间,很长的分钟分辨率)。
time
是开始时间,now
是结束时间(以毫秒为单位)。要报告“几秒钟前”,请将 minResolution 设置为零。例子:
Use DateUtils.getRelativeTimeSpanString(long time, long now, long minResolution).
time
is the start time, andnow
is the end time (in milliseconds). To report "seconds ago," set minResolution to zero.Example:
这将返回两个日期之间的时间(以毫秒为单位)。只需将其转换为您想要显示的内容(例如小时分钟秒)
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)
在 Android 上,使用:http://developer.android.com/reference/android/text/format/DateUtils.html#getRelativeTimeSpanString%28android.content.Context,%20long,%20boolean%29
On Android, use this: http://developer.android.com/reference/android/text/format/DateUtils.html#getRelativeTimeSpanString%28android.content.Context,%20long,%20boolean%29
看看PrettyTime!
另外,每次你想用 Java 做一些与日期/时间相关的事情时,你应该看看 Joda时间。现在就做吧,以后你会感谢我的。
Take a look at 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.
您的需求非常具体,我不知道有任何库可以立即为您解决问题。
然而问题并不是很复杂,一个充满“ifs”的小函数应该可以解决问题。
当然,像 Joda Time 这样的优秀日期库将帮助您保持代码整洁。谁想使用公历日历!?
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!?
看起来您有一组自定义规则,并且选择规则的算法基于两个时间戳之间的时间(以秒为单位)。最简单的方法是处理一系列 if/else if 语句中的规则:
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 rules in a series of if/else if statements: