五陵年少金市东,银鞍白马渡春风。这篇文章主要讲述timeago相当于Android相关的知识,希望能为你提供帮助。
在timeago的android中是否有相同的功能?
【timeago相当于Android】非常重要的是,日期不仅仅是“漂亮印刷”,而且随着时间的推移也会更新。
一些具有类似功能的项目/资源(如果我发现更多,我将更新此列表):
- android-timeago
- android DateUtils
- android-ago (Highly recommended)
人们可以通过传递毫秒来调用此方法来更新。您可以根据您的要求通过发送日期格式或日期字符串来更改它。
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应用程序中使用它。
链接:Timeago on Android arsenal
用法(Java):
long timeInMillis = System.currentTimeMillis();
String text = TimeAgo.using(timeInMillis);
推荐阅读
- 本地化不适用于App的初始加载
- 在Android和Raspberry Pi 3 Linux之间建立Wifi-Direct连接
- 通过Android应用程序向Apache服务器发送请求,摆脱无限循环
- MediaPlayer无法在Android Things Raspberry Pi 3上运行
- 在Android Things上扫描Wifi网络会返回空列表
- 如何在Raspberry Pi 3模块上启用I2C(与Android Things一起使用作为操作系统)
- 使用generator-fountain-webapp的Yoeman出错
- 找不到模块'wrappy'
- 如何在symfony中创建Twig扩展名以及如何使用Twig创建查询