如果目前还没有一种简单的方法,我只会构建功能,但我是Joda-Time的新手,并且想知道是否有一种简单的方法可以做到这一点:
基本上,您使用ISO8601格式的time
,并使用Joda-Time创建new DateTime(time);
。
现在,如果时间是5秒前,我希望能够阅读" 5s",如果是10分钟前," 10m",17小时前," 17h",3周前," 3w"。
显然,如果在79秒之前写了一些东西,它会意识到这超过1分钟,而是选择在几分钟内检查一下,而是说" 1m"而不是" 79s"。
更新
到目前为止,我的快速解决方案是:
public void setTimestamp() {
DateTime now = DateTime.now();
if (DateTimeComparator.getInstance().compare(timeCreated, now.minusSeconds(1)) == 1) {
timestamp = "1s";
} else if (DateTimeComparator.getInstance().compare(timeCreated, now.minusMinutes(1)) == 1) {
timestamp = Seconds.secondsBetween(timeCreated, now).getSeconds() + "s";
} else if (DateTimeComparator.getInstance().compare(timeCreated, now.minusHours(1)) == 1) {
timestamp = Minutes.minutesBetween(timeCreated, now).getMinutes() + "m";
} else if (DateTimeComparator.getInstance().compare(timeCreated, now.minusDays(1)) == 1) {
timestamp = Hours.hoursBetween(timeCreated, now).getHours() + "h";
} else if (DateTimeComparator.getInstance().compare(timeCreated, now.minusWeeks(1)) == 1) {
timestamp = Days.daysBetween(timeCreated, now).getDays() + "d";
} else if (DateTimeComparator.getInstance().compare(timeCreated, now.minusYears(1)) == 1) {
timestamp = Weeks.weeksBetween(timeCreated, now).getWeeks() + "w";
} else {
timestamp = Years.yearsBetween(timeCreated, now).getYears() + "y";
}
}
它仍然需要一些测试,但从最初的看起来,它似乎工作。