java的计算两个时间戳之间的时间 [英] java calculate time between two timestamps

查看:1335
本文介绍了java的计算两个时间戳之间的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to calculate the time passed between two dates.

下面美中不足的是,我需要表现出来如YouTube确实有它的视频评论时间戳。也就是说,仅通过最大的测量证明这一点。

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.

例如,

  • 如果时间为50秒前,应该说50秒前。
  • 如果该时间超过一分钟,应该说一分钟前/ 10分前等。
  • 如果时差为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)?

推荐答案

使用<一个href="http://developer.android.com/reference/android/text/format/DateUtils.html#getRelativeTimeSpanString%28long,%20long,%20long%29">DateUtils.getRelativeTimeSpanString(long时间长了,长minResolution)。 时间的开始时间,而现在的结束时间(毫秒)。要报告秒前,设置minResolution为零。

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.

例如:

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

这篇关于java的计算两个时间戳之间的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆