Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom format for relative time span

I'm trying to create an custom format to display the elapsed time.

Now I'm using:

CharSequence relativeTimeSpan = DateUtils.getRelativeTimeSpanString(
     dateObject.getTime(), 
     System.currentTimeMillis(), 
     DateUtils.SECOND_IN_MILLIS,
     flags);

This returns relative time spans like this:

  • 1 minute ago
  • 36 minutes ago
  • 4 hours ago

...

What I'm trying to do is display this, like this:

  • 1m
  • 36m
  • 4h

...

I know DateUtils has FORMAT_ABBREV_ALL flag, but this doesn't abbreviate the string like I want to...

How can I create something custom for this ?

like image 873
Ionut Negru Avatar asked Oct 16 '13 16:10

Ionut Negru


2 Answers

i have done this in my twitter module

public static String getTimeString(Date fromdate) {

    long then;
    then = fromdate.getTime();
    Date date = new Date(then);

    StringBuffer dateStr = new StringBuffer();

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    Calendar now = Calendar.getInstance();

    int days = daysBetween(calendar.getTime(), now.getTime());
    int minutes = hoursBetween(calendar.getTime(), now.getTime());
    int hours = minutes / 60;
    if (days == 0) {

        int second = minuteBetween(calendar.getTime(), now.getTime());
        if (minutes > 60) {

            if (hours >= 1 && hours <= 24) {
                dateStr.append(hours).append("h");
            }

        } else {

            if (second <= 10) {
                dateStr.append("Now");
            } else if (second > 10 && second <= 30) {
                dateStr.append("few seconds ago");
            } else if (second > 30 && second <= 60) {
                dateStr.append(second).append("s");
            } else if (second >= 60 && minutes <= 60) {
                dateStr.append(minutes).append("m");
            }
        }
    } else

    if (hours > 24 && days <= 7) {
        dateStr.append(days).append("d");
    } else {
        dateStr.append(twtimeformat.format(date));
    }

    return dateStr.toString();
}

public static int minuteBetween(Date d1, Date d2) {
    return (int) ((d2.getTime() - d1.getTime()) / DateUtils.SECOND_IN_MILLIS);
}

public static int hoursBetween(Date d1, Date d2) {
    return (int) ((d2.getTime() - d1.getTime()) / DateUtils.MINUTE_IN_MILLIS);
}

public static int daysBetween(Date d1, Date d2) {
    return (int) ((d2.getTime() - d1.getTime()) / DateUtils.DAY_IN_MILLIS);
}
like image 141
MilapTank Avatar answered Oct 13 '22 04:10

MilapTank


Create One method for get Date difference like this

public static String getDateDifferenceForDisplay(Date inputdate) {
    Calendar now = Calendar.getInstance();
    Calendar then = Calendar.getInstance();

    now.setTime(new Date());
    then.setTime(inputdate);

    // Get the represented date in milliseconds
    long nowMs = now.getTimeInMillis();
    long thenMs = then.getTimeInMillis();

    // Calculate difference in milliseconds
    long diff = nowMs - thenMs;

    // Calculate difference in seconds
    long diffMinutes = diff / (60 * 1000);
    long diffHours = diff / (60 * 60 * 1000);
    long diffDays = diff / (24 * 60 * 60 * 1000);

    if (diffMinutes < 60) {
        return diffMinutes + " m";

    } else if (diffHours < 24) {
        return diffHours + " h";

    } else if (diffDays < 7) {
        return diffDays + " d";

    } else {

        SimpleDateFormat todate = new SimpleDateFormat("MMM dd",
                Locale.ENGLISH);

        return todate.format(inputdate);
    }
} 

Then you can call it like this

String dateForDisplay = getDateDifferenceForDisplay(inputDate);
like image 34
Chirag Ghori Avatar answered Oct 13 '22 04:10

Chirag Ghori