Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying time ago like Twitter

I am using DateUtils.getRelativeTimeSpanString() for displaying when a post is made.

The string it returns is something like that: 1 minute ago, but I would like to be 1 m or 1 h and so on (shortened and without ago).

I can replace “hour” with “h” or “minute” with “m” but if the language is different from English it won’t work. It's what Twitter is using right now. English/Cyrillic way of displaying by Twitter.

enter image description here

enter image description here

UPDATE: I will accept the answer from @Bradley Wilson although I will add here cleaner solution (using again the JodaTime package). Also the rest of the answers also can be modified for the same result so they deserve up vote. Thank you, all:

    DateTime postMaded = new DateTime(your previous date);
    DateTime nowUpdate = new DateTime();

    Period period = new Period(postMaded, nowUpdate);

    PeriodFormatter formatter;

    Locale current = ConverterMethods.getCurrentLocale();

    if (current.getLanguage().contentEquals(any Cyrillic language )) {

        if (period.getYears() != 0) {
            formatter = new PeriodFormatterBuilder().appendYears().appendSuffix(" г.").printZeroNever().toFormatter();
        } else if (period.getMonths() != 0) {
            formatter = new PeriodFormatterBuilder().appendMonths().appendSuffix(" м").printZeroNever().toFormatter();
        } else if (period.getWeeks() != 0) {
            formatter = new PeriodFormatterBuilder().appendWeeks().appendSuffix(" седм.").printZeroNever().toFormatter();
        } else if (period.getDays() != 0) {
            formatter = new PeriodFormatterBuilder().appendDays().appendSuffix(" д").printZeroNever().toFormatter();
        } else if (period.getHours() != 0) {
            formatter = new PeriodFormatterBuilder().appendHours().appendSuffix(" ч").printZeroNever().toFormatter();
        } else if (period.getMinutes() != 0) {
            formatter = new PeriodFormatterBuilder().appendMinutes().appendSuffix(" мин").printZeroNever().toFormatter();
        } else {
            formatter = new PeriodFormatterBuilder().appendSeconds().appendSuffix(" с").printZeroNever().toFormatter();
        }

    } else {

        if (period.getYears() != 0) {
            formatter = new PeriodFormatterBuilder().appendYears().appendSuffix(" y").printZeroNever().toFormatter();
        } else if (period.getMonths() != 0) {
            formatter = new PeriodFormatterBuilder().appendMonths().appendSuffix(" mon").printZeroNever().toFormatter();
        } else if (period.getWeeks() != 0) {
            formatter = new PeriodFormatterBuilder().appendWeeks().appendSuffix(" w").printZeroNever().toFormatter();
        } else if (period.getDays() != 0) {
            formatter = new PeriodFormatterBuilder().appendDays().appendSuffix(" d").printZeroNever().toFormatter();
        } else if (period.getHours() != 0) {
            formatter = new PeriodFormatterBuilder().appendHours().appendSuffix(" h").printZeroNever().toFormatter();
        } else if (period.getMinutes() != 0) {
            formatter = new PeriodFormatterBuilder().appendMinutes().appendSuffix(" m").printZeroNever().toFormatter();
        } else {
            formatter = new PeriodFormatterBuilder().appendSeconds().appendSuffix(" s").printZeroNever().toFormatter();
        }
    }
like image 816
charbinary Avatar asked Jan 04 '23 19:01

charbinary


2 Answers

Take a look at the PrettyTime library.

It's quite simple to use:

import org.ocpsoft.prettytime.PrettyTime

PrettyTime p = new PrettyTime();
System.out.println(p.format(new Date()));
// prints "moments ago"

ou can also pass in a locale for internationalized messages:

PrettyTime p = new PrettyTime(new Locale("fr"));
System.out.println(p.format(new Date()));
// prints "à l'instant"

As noted in the comments, Android has this functionality built into the android.text.format.DateUtils class.

like image 98
Jaydip Umaretiya Avatar answered Jan 08 '23 07:01

Jaydip Umaretiya


you can use this function to do that

  public static String getTimeAgo(long time) {
     if (time < 1000000000000L) {
     // if timestamp given in seconds, convert to millis
     time *= 1000;
  }

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

  // TODO: localize
  final long diff = now - time;
  if (diff < MINUTE_MILLIS) {
     return "just now";
  } else if (diff < 2 * MINUTE_MILLIS) {
     return "a minute ago";
  } else if (diff < 50 * MINUTE_MILLIS) {
     return diff / MINUTE_MILLIS + " min ago";
  } else if (diff < 90 * MINUTE_MILLIS) {
     return "an hour ago";
  } else if (diff < 24 * HOUR_MILLIS) {
     return diff / HOUR_MILLIS + " hours ago";
  } else if (diff < 48 * HOUR_MILLIS) {
     return "yesterday";
  } else {
     return diff / DAY_MILLIS + " days ago";
  }
 }
like image 31
Amir_P Avatar answered Jan 08 '23 05:01

Amir_P