18

Can someone show me an example how to properly use the getRelativeDateTimeString() that is detailed here.

Phoenix
  • 1,329
  • 2
  • 12
  • 20
Nick
  • 9,285
  • 33
  • 104
  • 147

2 Answers2

47

I guess you are talking about getRelativeDateTimeString, which is pointed to by your link.

Example for now, with comments detailing all params:

Date now = new Date();
String str = DateUtils.getRelativeDateTimeString(

        this, // Suppose you are in an activity or other Context subclass

        now.getTime(), // The time to display

        DateUtils.MINUTE_IN_MILLIS, // The resolution. This will display only 
                                        // minutes (no "3 seconds ago") 


        DateUtils.WEEK_IN_MILLIS, // The maximum resolution at which the time will switch 
                         // to default date instead of spans. This will not 
                         // display "3 weeks ago" but a full date instead

        0); // Eventual flags

Other values for MINUTE_IN_MILLIS and YEAR_IN_MILLIS include:

  • SECOND_IN_MILLIS
  • MINUTE_IN_MILLIS
  • HOUR_IN_MILLIS
  • DAY_IN_MILLIS
  • WEEK_IN_MILLIS
  • YEAR_IN_MILLIS
  • Any custom value in milliseconds
PhilLab
  • 4,777
  • 1
  • 25
  • 77
Vivien Barousse
  • 20,555
  • 2
  • 63
  • 64
  • 6
    Did you try with WEEK_IN_MILLIS and YEAR_IN_MILLIS? I cannot make it work, here are my code: `code` time = now - 7*24*60*60*1000; text += DateUtils.getRelativeDateTimeString(this, time, DateUtils.MINUTE_IN_MILLIS, DateUtils.YEAR_IN_MILLIS, 0) + "\n"; `code`. And I wonder why it does not support MONTH_IN_MILLIS – thanhbinh84 Feb 12 '12 at 02:55
  • 5
    Why `DateTimeUtils` (from Joda Time?) instead of `DateUtils` ([part of android](http://developer.android.com/reference/android/text/format/DateUtils.html))? – cdunn2001 Aug 28 '14 at 07:21
  • 1
    You can't get have a resolution greater than one week, see [`RelativeDateTimeFormatter`](https://android.googlesource.com/platform/libcore/+/a40d244/luni/src/main/java/libcore/icu/RelativeDateTimeFormatter.java#274), used by DateUtils. – Nicolas Sep 13 '18 at 01:24
  • Why do I keep getting "in 1 hour" instead of "1 hour ago"? I'm using UTC. Is my app using or ignoring the time zone? If I don't use UTC (which my server returns), I get a wrong time. WTF. It's driving me nuts. – TheRealChx101 Mar 13 '19 at 17:05
4

I think a better question would specify what you want as an output. However, here's an example:

 DateUtils.getRelativeTimeSpanString(yourContext, theEventInMillis, 
                                     DateUtils.MINUTE_IN_MILLIS, 
                                     DateUtils.FORMAT_NO_NOON);

This will format theEventInMillis relative to whatever the system's current time is. It will show changes in minutes (0 minutes ago, 2 minutes ago, 3 hours ago, 1 day ago, etc) until the difference reaches a week, then it will just post the full date. The flags field (0 in this case, last argument) you can use to control how the resulting string is rendered, but you should check the docs to see what fits your needs.

dmon
  • 30,048
  • 8
  • 87
  • 96
  • 1
    Hi @dmon, there is no method signature for getRelativeTimeSpanString that matches the arguments in your example. That does match one of the methods for getRelativeDateTimeString. – Adrian Carr Mar 21 '12 at 12:52