3

I'm trying to use this Android helper method from DateUtils :

public static CharSequence getRelativeTimeSpanString (long time, long now, long minResolution, int flags)

In the documentation they say :

Can use FORMAT_ABBREV_RELATIVE flag to use abbreviated relative times, like "42 mins ago".

But I can't find they way to make FORMAT_ABBREV_RELATIVE work... I always get "42 minutes ago", my String is never abbreviated.

Here is my code :

DateUtils.getRelativeTimeSpanString(
    message.getCreationDateTime().getTime(),
    System.currentTimeMillis(),
    DateUtils.MINUTE_IN_MILLIS,
    DateUtils.FORMAT_ABBREV_RELATIVE
)

Can you please tell me what I'm doing wrong ?

Thank you in advance.

Edit :

In fact, I found out it is working when my phone is in English, but not in French, I get "I y a 42 minutes", which is very long. Do anyone know a solution that work on multiple languages ?

victorleduc
  • 232
  • 2
  • 9

1 Answers1

1

What's your device language setting? I'm using English (United States) and with the following code I get "42 mins ago"

DateUtils.getRelativeTimeSpanString(
System.currentTimeMillis() - 42 * 60 * 1000,
System.currentTimeMillis(),
DateUtils.MINUTE_IN_MILLIS,
DateUtils.FORMAT_ABBREV_RELATIVE)
Xiaoyu Yu
  • 725
  • 3
  • 12
  • I checked, and your right, it's working in english ! When my phone is in french, it does not work, it say "Il y a 42 minutes" wich is very long... Do you know à workarround for this ? – victorleduc Oct 31 '15 at 08:05
  • This kind of API will always adapt the OS language setting, if you want it to always show English, you can either implement this function yourself, or overwrite the locale in your app. You can check this link: http://stackoverflow.com/a/26864311/4805308 – Xiaoyu Yu Nov 01 '15 at 01:28
  • You answer is interesting, I think I will use it for other languages. But, my main issue is that I can't do it in french, the main language of my app. – victorleduc Nov 01 '15 at 10:22
  • Like I mentioned above, you can either implement the function yourself, which converts a time stamp to an English string. It's not quite hard. – Xiaoyu Yu Nov 01 '15 at 16:13