因此,Android SDK 中的 [DateUtils.getRelativeTimeSpanString()][1] 非常适合显示过去的相对时间。
即:5 天前,或 5 分钟前。
但对于未来的日期,似乎效果不佳。它似乎只是打印日期。
是否有任何简单的替代方法可以为将来的日期生成相对时间跨度字符串(通过比较两个日历对象来编写一些计算出天、小时、分钟、秒的东西)?
排长队的东西:在 5 天内,还是在 5 分钟内?
这基本上是我必须要做的,它看起来有点脏(注意:这段代码只是为帖子编写的,实际上并没有通过 java 编译器运行):
Calendar calendarIO = Calendar.getInstance();
calendarIO.set(2013, 2, 14, 7, 0);
long milliseconds1 = calendarIO.getTimeInMillis();
long milliseconds2 = System.currentTimeMillis();
long diff = milliseconds1 - milliseconds2;
long diffSeconds = diff / 1000;
long diffMinutes = diff / (60 * 1000);
long diffHours = diff / (60 * 60 * 1000);
long diffDays = diff / (24 * 60 * 60 * 1000);
String relativeTime = "";
if (diffDays > 1) {
relativeTime = diffDays + " days";
} else if (diffDays > 0) {
relativeTime = diffDays + " days " + diffHours + " hours";
} else if (diffHours > 1) {
relativeTime = diffHours + " hours";
} else if (diffMinutes > 0) {
relativeTime = diffMinutes + " minutes.";
}