timeago相当于Android

时间:2013-09-02 22:24:51

标签: android date localization timeago

Android中是否有timeago的等价物?

非常重要的是,日期不仅仅是“漂亮印刷”,而且随着时间的推移也会更新。

一些具有类似功能的项目/资源(如果我找到更多,我会更新此列表):

2 个答案:

答案 0 :(得分: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;
    }
}

答案 1 :(得分:1)

问候@fernandohur,

我已经发布了这样一个满足您要求的图书馆。

TimeAgo 是一个简单的java库,它将日期/时间转换为相对“'time ago'或'time time'文本格式。

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

link:Timeago on Android arsenal

用法(Java):

long timeInMillis = System.currentTimeMillis();
String text = TimeAgo.using(timeInMillis);