Here you can find the source of timeAgo(Date d)
public static String timeAgo(Date d)
//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); } }