java计算两个时间戳之间的时间

发布于 2024-11-11 17:08:29 字数 411 浏览 7 评论 0原文

我需要计算两个日期之间经过的时间。

这里的问题是我需要像 YouTube 那样显示它的视频评论时间戳。也就是说,仅用最大的度量来显示它。

例如,

  • 如果时间是 50 秒前,则应该说 50 秒前。
  • 如果时间超过一分钟,则应显示一分钟前/十分钟前等。
  • 如果时差为 1 小时 30 分钟,则应显示:一小时前。
  • 如果时间是一周半,则应该是一周前。
  • 如果时间超过一个月,则应该说一个月前/两个月前等等......等等
  • ......

那么处理这个问题的最佳方法是什么? 我应该使用 caseif 语句创建一个返回类似内容的方法吗?或者是否有更好的方法(也许一个库已经做了类似的事情)?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

输什么也不输骨气 2024-11-18 17:08:29

使用 DateUtils.getRelativeTimeSpanString(很长一段时间,很长一段时间,很长的分钟分辨率)time 是开始时间,now 是结束时间(以毫秒为单位)。要报告“几秒钟前”,请将 minResolution 设置为零。

例子:

String result = DateUtils.getRelativeTimeSpanString(1306767830, 1306767835, 0);
// result = "5 seconds ago"

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.

Example:

String result = DateUtils.getRelativeTimeSpanString(1306767830, 1306767835, 0);
// result = "5 seconds ago"
在风中等你 2024-11-18 17:08:29
 date1.getTime() - date2.getTime() 

这将返回两个日期之间的时间(以毫秒为单位)。只需将其转换为您想要显示的内容(例如小时分钟秒)

 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)

阪姬 2024-11-18 17:08: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.

小清晰的声音 2024-11-18 17:08:29

您的需求非常具体,我不知道有任何库可以立即为您解决问题。
然而问题并不是很复杂,一个充满“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!?

夜雨飘雪 2024-11-18 17:08:29

看起来您有一组自定义规则,并且选择规则的算法基于两个时间戳之间的时间(以秒为单位)。最简单的方法是处理一系列 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
 }

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:

 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
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文