Android Date Interval Get timeAgo(Date d)

Here you can find the source of timeAgo(Date d)

Description

time Ago

Declaration

public static String timeAgo(Date d) 

Method Source Code

//package com.java2s;
import android.text.format.DateUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    private static final String TIMEAGO_JUST_NOW = "just now";
    private static final String TIMEAGO_HOURS_AGO = "%d hours ago";
    private static final String TIMEAGO_MINUTES_AGO = "%d mins ago";

    public static String timeAgo(Date d) {
        final long referenceTime = d.getTime();
        final long nowTime = System.currentTimeMillis();
        final long diffTime = Math.abs(nowTime - referenceTime);
        if (diffTime > DateUtils.DAY_IN_MILLIS) {
            return format(d, "yyyy-MM-dd HH:mm");
        } else if (diffTime > DateUtils.HOUR_IN_MILLIS) {
            return String.format(TIMEAGO_HOURS_AGO, diffTime
                    / DateUtils.HOUR_IN_MILLIS);
        } else if (diffTime > DateUtils.MINUTE_IN_MILLIS) {
            return String.format(TIMEAGO_MINUTES_AGO, diffTime
                    / DateUtils.MINUTE_IN_MILLIS);
        } else {/* w w  w  . j  av  a2 s. co m*/
            return TIMEAGO_JUST_NOW;
        }
    }

    public static String format(Date d, String dateFormat) {
        final SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        return sdf.format(d);
    }
}

Related

  1. getStringByOffset(Date date, String format, int calendarField, int offset)
  2. getStringByOffset(Date date, String format, int calendarField, int offset)
  3. getStringByOffset(String strDate, String format, int calendarField, int offset)
  4. getStringByOffset(String strDate, String format, int calendarField, int offset)
  5. getDaysSince(Date date)