timeago相当于Android
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了timeago相当于Android相关的知识,希望对你有一定的参考价值。
非常重要的是,日期不仅仅是“漂亮印刷”,而且随着时间的推移也会更新。
一些具有类似功能的项目/资源(如果我发现更多,我将更新此列表):
答案
这是您的TimeAgo方法。
人们可以通过传递毫秒来调用此方法来更新。您可以根据您的要求通过发送日期格式或日期字符串来更改它。
public class TimeAgo {
public final static String monthAgo = " month ago";
public final static String weekAgo = " week ago";
public final static String daysAgo = " days ago";
public final static String hoursAgo = " hour ago";
public final static String minAgo = " minute ago";
public final static String secAgo = " seconds ago";
static int second = 1000; // milliseconds
static int minute = 60;
static int hour = minute * 60;
static int day = hour * 24;
static int week = day * 7;
static int month = day * 30;
static int year = month * 12;
@SuppressLint("SimpleDateFormat")
public static String DateDifference(long fromDate) {
long diff = 0;
long ms2 = System.currentTimeMillis();
// get difference in milli seconds
diff = ms2 - fromDate;
int diffInSec = Math.abs((int) (diff / (second)));
String difference = "";
if(diffInSec < minute)
{
difference = diffInSec+secAgo;
}
else if((diffInSec / hour) < 1)
{
difference = (diffInSec/minute)+minAgo;
}
else if((diffInSec/ day) < 1)
{
difference = (diffInSec/hour)+hoursAgo;
}
else if((diffInSec/ week) < 1)
{
difference = (diffInSec/day)+daysAgo;
}
else if((diffInSec/month)<1)
{
difference = (diffInSec / week)+weekAgo;
}
else if((diffInSec/year)<1)
{
difference = (diffInSec / month)+monthAgo;
}
else
{
// return date
Calendar c = Calendar.getInstance();
c.setTimeInMillis(fromDate);
SimpleDateFormat format_before = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
difference = format_before.format(c.getTime());
}
Log.e("time difference is: ","" + difference);
return difference;
}
}
另一答案
问候@fernandohur,
我已经发布了这样一个满足您要求的库。
TimeAgo是一个简单的Java库,它将日期/时间转换为相对“时间前”或“时间内”文本格式。
它使用纯Java日期/时间处理,因此,它没有joda-time api等依赖,因此,你可以在android应用程序中使用它。
用法(Java):
long timeInMillis = System.currentTimeMillis();
String text = TimeAgo.using(timeInMillis);
以上是关于timeago相当于Android的主要内容,如果未能解决你的问题,请参考以下文章
****timeago.js插件:jquery实现几分钟前几小时前几天前等时间差显示效果的代码实例