0

timeago在 Android 中是否有等价物?

日期不仅“漂亮地打印”而且随着时间的推移而更新,这一点非常重要。

一些具有类似功能的项目/资源(如果我发现更多,我会更新这个列表):

4

2 回答 2

2

这是您的 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;
    }
}
于 2015-04-08T10:58:12.600 回答
1

问候@fernandohur,

我已经发布了这样一个可以满足您要求的库。

TimeAgo是一个简单的 java 库,可将日期/时间转换为相对“时间前”或“时间内”文本格式。

它使用纯 java 日期/时间处理,因此没有 joda-time api 等依赖,因此您可以在 android 应用程序中使用它。

链接:Android 阿森纳上的 Timeago

用法(Java):

long timeInMillis = System.currentTimeMillis();
String text = TimeAgo.using(timeInMillis);
于 2016-11-23T15:33:30.143 回答