简体   繁体   中英

Converting a timestamp to relative time

I want convert timestamp to relative time using Java in Android like Facebook . I'm using android.text.format.DateUtils.getRelativeTimeSpanString() but not getting expected result.

My code :

public static CharSequence  getTimeAgo(long timestamp) {

    return DateUtils.getRelativeTimeSpanString(timestamp, System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS, DateUtils.FORMAT_ABBREV_RELATIVE);

}

Results :

5 sec ago
5 min ago
5 hr ago
5 days ago

Expected results :

1 sec
5 secs
1 min
5 mins
1 hr
5 hrs
5 days
Yesterday at 11:48 pm
27 Jan at 6:52 pm
25 Aug 2019 at 7:53 pm

Use this function :

You just need to pass timestamp here

   public static String dayStringFormat(long msecs) {
        final long ONE_SECOND = 1000L;
        final long ONE_MINUTE = 60000L;
        final long ONE_HOUR = 3600000L;
        final long ONE_DAY = 86400000L;
        final long ONE_MONTH = 2592000000L;
        final long ONE_YEAR = 31536000000L;
        int gmtOffset = TimeZone.getDefault().getRawOffset();
        long currentTime = System.currentTimeMillis();
        long difference = currentTime - msecs;
        if (difference < ONE_MINUTE) {
            long timeAgo = difference / ONE_SECOND;
            int finalUnits = (int) timeAgo;
            return "Just Now"
        } else if (difference < ONE_HOUR) {
            long timeAgo = difference / ONE_MINUTE;
            int finalUnits = (int) timeAgo;
            return  finalUnits + " " + "Minutes Ago";
        } else if (difference < ONE_DAY) {
            long timeAgo = difference / ONE_HOUR;
            int finalUnits = (int) timeAgo;
            return finalUnits + " " + "Hours Ago";
        } else if (difference < ONE_MONTH) {
            long timeAgo = difference / ONE_DAY;
            int finalUnits = (int) timeAgo;
            return  finalUnits +" " + "Days Ago";
        } else if (difference < ONE_YEAR) {
            long timeAgo = difference / ONE_MONTH;
            int finalUnits = (int) timeAgo;
            return  finalUnits + " " + "Months Ago";
        } else {
            long timeAgo = difference / ONE_MONTH;
            int finalUnits = (int) timeAgo;
            return  finalUnits + "" + "Months Ago";

        }
    }

Hope this will work!

This function will return time same as Facebook :

public static String getTimeAgo(long time) {

    final DateFormat sdfTime = new SimpleDateFormat("h:mm aa", Locale.ENGLISH);
    final DateFormat sdf = new SimpleDateFormat("d MMM 'at' h:mm aa", Locale.ENGLISH);
    final DateFormat sdfY = new SimpleDateFormat("d MMM yyyy 'at' h:mm aa", Locale.ENGLISH);

    final int SECOND_MILLIS = 1000;
    final int MINUTE_MILLIS = 60 * SECOND_MILLIS;
    final int HOUR_MILLIS = 60 * MINUTE_MILLIS;
    final int DAY_MILLIS = 24 * HOUR_MILLIS;

    if (time < 1000000000000L) {
        time *= 1000;
    }

    Calendar today = Calendar.getInstance();

    Calendar timeCal = Calendar.getInstance();
    timeCal.setTimeInMillis(time);

    long now = System.currentTimeMillis();
    if (time > now || time <= 0) {
        return null;
    }

    final long diff = now - time;
    if (diff < MINUTE_MILLIS) {
        return "Just now";
    } else if (diff < 2 * MINUTE_MILLIS) {
        return "1 min";
    } else if (diff < 59 * MINUTE_MILLIS) {
        return diff / MINUTE_MILLIS + " mins";
    } else if (diff < 2 * HOUR_MILLIS) {
        return "1 hr";
    } else if (diff < 24 * HOUR_MILLIS) {
        return diff / HOUR_MILLIS + " hrs";
    } else if (diff < 48 * HOUR_MILLIS) {
        return ("Yesterday at " + capsAMtoSmall(sdfTime.format(timeCal.getTime())));
    } else if (today.get(Calendar.YEAR) == timeCal.get(Calendar.YEAR)) {
        return capsAMtoSmall(sdf.format(timeCal.getTime()));
    } else {
        return capsAMtoSmall(sdfY.format(timeCal.getTime()));
    }
}

private static String capsAMtoSmall(String time) {
    return time.replace("AM", "am").replace("PM","pm");
}

Happy coding :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM