我是java和android的新手.我的一个Android新闻应用程序我将时间显示为"2014年8月7日上午8:20"
但我需要显示它:
5 mins ago
1 hour ago
2 days ago
Run Code Online (Sandbox Code Playgroud)
发现很多图书馆,比如我们美好的时光,joda.但我不知道如何将其添加到我的Android应用程序.甚至这个链接/sf/answers/911305321/告诉我.但是如何将我的日期和时间传递给它.
任何简单的代码都可以做到.
谢谢
Rit*_*une 44
您要显示的内容称为相对时间显示.Android提供了显示相对于当前时间的时间的方法.您不必为此目的使用任何第三方库.
您可以使用
DateUtils.getRelativeTimeSpanString(long time, long now, long minResolution)
Run Code Online (Sandbox Code Playgroud)
例如.
DateUtils.getRelativeTimeSpanString(your_time_in_milliseconds, current_ time_in_millisecinds,DateUtils.MINUTE_IN_MILLIS);
Run Code Online (Sandbox Code Playgroud)
UPDATE1:
您可以尝试按照传递日期并获得毫秒数.
public static long getDateInMillis(String srcDate) {
SimpleDateFormat desiredFormat = new SimpleDateFormat(
"d MMMM yyyy, hh:mm aa");
long dateInMillis = 0;
try {
Date date = desiredFormat.parse(srcDate);
dateInMillis = date.getTime();
return dateInMillis;
} catch (ParseException e) {
Log.d("Exception while parsing date. " + e.getMessage());
e.printStackTrace();
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
希望它能帮到你.
创建以下逻辑:
% it by 3600*365
result > 0那么显示年前else % it by 3600 * 30
result > 0那么显示月份之前else % it by 3600 * 7
result > 0那么显示周前else % it by 3600
result > 0那么显示前一天else % it by 3600 / 24
result > 0然后显示小时前else % it by 60,
result > 0然后显示分钟前注意:%表示mod(模数运算)
| 归档时间: |
|
| 查看次数: |
14199 次 |
| 最近记录: |