时间前为 Android/java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25174921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Time ago for Android/java
提问by binu j
I am newbie to java and android. One of my android news app i am displaying the time as "7 August 2014, 8:20 am"
我是java和android的新手。我的一款 Android 新闻应用程序将时间显示为“2014 年 8 月 7 日,上午 8:20”
But I need to display it like:
但我需要像这样显示它:
5 mins ago
1 hour ago
2 days ago
Found many libraries such us pretty time, joda. But i dont know how to add it to my android app. Even this link https://stackoverflow.com/a/13018647/2020685show me. But how to pass my date and time into it.
发现很多图书馆,比如我们漂亮的时候,joda。但我不知道如何将它添加到我的 android 应用程序中。甚至这个链接https://stackoverflow.com/a/13018647/2020685 也给我看。但是如何将我的日期和时间传递给它。
Any simple code to do it.
任何简单的代码来做到这一点。
Thanks
谢谢
回答by Ritesh Gune
What you want to display is called as the Relative time display. Android provides methods to display time relative to your current time. You don't have to use any third party library just for this purpose.
您要显示的内容称为相对时间显示。Android 提供了显示相对于您当前时间的时间的方法。您不必为此目的使用任何第三方库。
You can use
您可以使用
DateUtils.getRelativeTimeSpanString(long time, long now, long minResolution)
Refer docs Here
在这里参考文档
eg.
例如。
DateUtils.getRelativeTimeSpanString(your_time_in_milliseconds, current_ time_in_millisecinds,DateUtils.MINUTE_IN_MILLIS);
UPDATE1:
更新1:
You can try following to pass your date and get the milliseconds for it.
您可以尝试按照以下方式传递您的日期并获取其毫秒数。
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;
}
Hope it helps you.
希望对你有帮助。
回答by Rudy
Create the below logic:
创建以下逻辑:
Convert your time to seconds, get the diff ( delta ) from current file :
将您的时间转换为秒,从当前文件中获取差异(delta):
% it by 3600*365
- if the
result > 0
then display year(s) ago
- if the
else % it by 3600 * 30
- if the
result > 0
then display month(s) ago
- if the
else % it by 3600 * 7
- if the
result > 0
then display week(s) ago
- if the
else % it by 3600
- if the
result > 0
then display day(s) ago
- if the
else % it by 3600 / 24
- if the
result > 0
then display hour(s) ago
- if the
else % it by 60
,- if the
result > 0
then display minute(s) ago
- if the
% it by 3600*365
- 如果
result > 0
then 显示年前
- 如果
else % it by 3600 * 30
- 如果
result > 0
then 显示一个月前
- 如果
else % it by 3600 * 7
- 如果
result > 0
then 显示一周前
- 如果
else % it by 3600
- 如果
result > 0
then 显示天前
- 如果
else % it by 3600 / 24
- 如果
result > 0
then 显示小时前
- 如果
else % it by 60
,- 如果
result > 0
then 显示分钟前
- 如果
NOTE:% means mod (modulus operation)
注意:% 表示 mod(模数运算)
回答by Jaskey
Date nowDate=new Date();
Date yourPassDate=//you have a date here.
long now=nowDate.getTime();
long time=yourPassDate.getTime();
final long diff = now - time; //now you have a date interval representing with mileseconds.
//you can use this diff to do something like:
if (diff <1000*60)//less than one minute
//...
else if (diff <1000*60*60) //less than 1 hour
//...
else if (diff < 1000*60*60*24)//less than one day
//...
This is what java doc of Date.getTime() said:
这是 Date.getTime() 的 java doc 所说的:
* Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT * represented by this <tt>Date</tt> object.
* Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT * represented by this <tt>Date</tt> object.