Right way to format date with strings like today, yesterday, tomorrow etc

前端 未结 6 570
臣服心动
臣服心动 2021-02-04 00:31

I have a date textview. And my textview holds a date string like 2011.09.17. Well I still do want to have that but I also want to add some more user friendly info for some speci

6条回答
  •  无人共我
    2021-02-04 01:06

    getRelativeTimeSpanString added in API level 3

    getRelativeTimeSpanString (long time, long now, long minResolution)

    Returns a string describing 'time' as a time relative to 'now'.

    Time spans in the past are formatted like "42 minutes ago". Time spans in the future are formatted like "In 42 minutes". More information you can find here:

    https://developer.android.com/reference/android/text/format/DateUtils

        Calendar now = Calendar.getInstance();
        return DateUtils.getRelativeTimeSpanString(time, now.getTimeInMillis(), DateUtils.DAY_IN_MILLIS);
    

提交回复
热议问题