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.
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();
}
}
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.
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";
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With