javaandroiddateandroid-dateutilsapache-commons-dateutils

How to show future date using DateUtils


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();

Solution

  • 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