DateUtils.getRelativeTimeSpanString用于将来的日期 [英] DateUtils.getRelativeTimeSpanString for future dates

查看:58
本文介绍了DateUtils.getRelativeTimeSpanString用于将来的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,Android SDK中的[DateUtils.getRelativeTimeSpanString()] [1]非常适合显示过去的相对时间.

So [DateUtils.getRelativeTimeSpanString()][1] in the Android SDK works great for showing relative times that are in the past.

即:5天前或5分钟前.

i.e: 5 days ago, or 5 minutes ago.

但是对于将来的日期来说,效果似乎不太好.看来只是打印日期.

But doesn't seem to work so well for dates that are in the future. It seems to just print the date.

是否有其他简便的方法可以为将来的日期生成相对时间跨度字符串(缺少通过比较两个日历对象来计算出日,时,分,秒的内容)?

Are there any easy alternatives for generating relative time span strings for dates that are in the future (short of writing something that figures out the days, hours, minutes, seconds by comparing two calendar objects)?

是5天还是5分钟之内的长行?

Something a long the lines of: in 5 days, or in 5 minutes?

这基本上是我必须要做的,而且看起来有点脏(请注意:此代码仅是为帖子编写的,实际上并未通过Java编译器运行):

Here is basically what I would have to make, and it just looks kind of dirty (note: this code was written just for the post and not actually run through a java compiler):

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.";
}

推荐答案

我会尝试使用 DateUtils.getRelativeTimeSpanString(time,now,minResolution),因为它可以使用"in n天"格式.

I'd try using DateUtils.getRelativeTimeSpanString(time, now, minResolution) as it works for future dates using an "in n days" format.

http://developer.android.com/reference/android/text/format/DateUtils.html#getRelativeTimeSpanString

这篇关于DateUtils.getRelativeTimeSpanString用于将来的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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