Nick Nick - 9 months ago 83
Android Question

Android: getRelativeTime example

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

Answer Source

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:

  • Any custom value in milliseconds