在android getRelativeTimeSpanString()
中使用内置的DateUtil
方法我无法得到“几分钟前”“几天前”等时间文件说我应该收到的时间已经过了响应,而是我得到一个只显示实际日期的结果像“2010年12月12日”这样的方式,不确定这是一个bug还是我曾尝试过getRelativeTimeSpanString (long time, long now, long minResolution)
和getRelativeTimeSpanString (long startTime)
,两者都只返回变量的实际日期“长时间”
这是我的代码
private void setJoineddate() {
Date currentDate = new Date();
long currentDateLong = currentDate.getTime();
long oldDate = join_date.getTime();
CharSequence relativeTime = DateUtils
.getRelativeTimeSpanString(oldDate, currentDateLong, 0);
joindate.setText(relativeTime);
}
不是错误。 getRelativeTimeSpanString
只会给你一个相对时间字符串,最长可达一周的时间。因此,如果您使用DateUtils.getRelativeTimeSpanString(time2, now, 0L, DateUtils.FORMAT_ABBREV_RELATIVE)
具有以下值
long now = System.currentTimeMillis();
long time2 = now - DateUtils.WEEK_IN_MILLIS;
如果今天是1 October
,你会得到8 October
,但是
long now = System.currentTimeMillis();
long time2 = now - DateUtils.WEEK_IN_MILLIS + 1;
会给你7 days ago
。
试试这个代码
DateUtils.getRelativeTimeSpanString(oldDate, currentDateLong,
0L, DateUtils.FORMAT_ABBREV_ALL);
它将返回Just now
,3 minutes ago
等。阅读文档并使用第三个参数来修改函数返回的字符串。
我有同样的问题。我在模拟器中测试我的应用程序,但后来我决定在我的nexus 7上安装应用程序只是为了看看它在设备上的外观和相对时间很好。
事实证明,模拟器的日期/时间仍设置为模拟器创建的原始日期。我想这是因为我从快照启动,但由于某种原因,自动更新日期/时间不起作用。我手动改变了模拟器中的时间,神奇地说,相对时间是预期的。
别忘了改变时区。
我的设备上也出现了这个问题,因为未设置设备时钟并显示1. 1970年1月。设置正确的日期后,它工作。