2

I'm creating app. It's a news list where on every news I need to show date, e.g. 9 hours ago or 1 month ago and so on. Also I have future news, so I need also to show e.g. after 1 month. So I'm using DateUtils.getRelativeTimeSpanString() for this purpose. But I only can show past dates, e.g. 9 hours ago or 1 month ago and I can't show future dates. It shows the future date like Jun 26, 2020. So is there a way to show it e.g. after 2 month using DateUtils?

DateUtils.getRelativeTimeSpanString(
            millis,
            System.currentTimeMillis(),
            DateUtils.MINUTE_IN_MILLIS).toString();
Hayk Mkrtchyan
  • 2,835
  • 3
  • 19
  • 61
  • See https://stackoverflow.com/questions/15216141/dateutils-getrelativetimespanstring-for-future-dates – soulcoder Apr 10 '20 at 18:31
  • That's cool, but what means the "in n days" in accepted answer? – Hayk Mkrtchyan Apr 10 '20 at 18:34
  • No I just simply can't understand what means the "in n days" in that accepted answer – Hayk Mkrtchyan Apr 10 '20 at 19:02
  • According to the linked documentation (in the accepted answer) `Time spans in the future are formatted like "In 42 minutes"`. If you want days, you can use `DateUtils.getRelativeTimeSpanString(time, now, DateUtils.DAY_IN_MLLIS)` – Bö macht Blau Apr 10 '20 at 19:37

1 Answers1

1

You can you same way but time spans in the future are formatted like "In 42 minutes"

DateUtils.getRelativeTimeSpanString(
            futureTimeInMillis,
            System.currentTimeMillis(),
            DateUtils.MINUTE_IN_MILLIS).toString();

You can pass one of 0, MINUTE_IN_MILLIS, HOUR_IN_MILLIS, DAY_IN_MILLIS, WEEK_IN_MILLIS in last parameter. Last parameter is the minimum resolution means by which format you want.

MINUTE_IN_MILLIS - In 42 minutes

HOUR_IN_MILLIS - In 2 hours

DAY_IN_MILLIS - In 2 days

Eklavya
  • 17,618
  • 4
  • 28
  • 57
  • Actually I will accept your asnwer. I used DateUtils.FORMAT_SHOW_WEEKDAY. If I will select DateUtils.MINUTE_IN_MILLIS, can I see my past dates like 2 days ago or 9 hours ago? – Hayk Mkrtchyan Apr 11 '20 at 19:00