How to Convert Time in Milliseconds into Relative Time Format in Android

Relative time format like “moments ago” or “5 minutes ago” or “one month ago” like those in Facebook, Twitter or other social media makes the time information easy to read and human readable. In Android we can use built in API DateUtils or external java library Prettytime to implement this feature.

Using DateUtils

By using method getRelativeDateTimeString(Context c, long time, long minResolution, long transitionResolution, int flags) in DateUtils class. This method available since API level 3, Android 1.5

long now 			= System.currentTimeMillis();
long secondsAgo		= now - 3000;
long minutesAgo		= now - 300000;
long hoursAgo		= now - 3600000;
long monthsAgo		= timeStringtoMilis("2014-01-02 00:00:00");
long longTimeAgo	= timeStringtoMilis("2004-05-12 09:33:12");

textView1.setText(DateUtils.getRelativeDateTimeString(this, secondsAgo, DateUtils.SECOND_IN_MILLIS, DateUtils.WEEK_IN_MILLIS, DateUtils.FORMAT_ABBREV_ALL));
textView2.setText(DateUtils.getRelativeDateTimeString(this, minutesAgo, DateUtils.SECOND_IN_MILLIS, DateUtils.WEEK_IN_MILLIS, DateUtils.FORMAT_ABBREV_ALL));
textView3.setText(DateUtils.getRelativeDateTimeString(this, hoursAgo, DateUtils.SECOND_IN_MILLIS, DateUtils.WEEK_IN_MILLIS, DateUtils.FORMAT_ABBREV_ALL));
textView4.setText(DateUtils.getRelativeDateTimeString(this, monthsAgo, DateUtils.SECOND_IN_MILLIS, DateUtils.WEEK_IN_MILLIS, DateUtils.FORMAT_ABBREV_ALL));
textView5.setText(DateUtils.getRelativeDateTimeString(this, longTimeAgo, DateUtils.SECOND_IN_MILLIS, DateUtils.WEEK_IN_MILLIS, DateUtils.FORMAT_ABBREV_ALL));

The results:

getRelativeDateTimeString

Using Prettytime

Prettytime is an open source formatting time library from ocpsoft.org to create a human readable relative time format. As the name implied, this library really makes the time format very pretty and friendly just like those we’ve seen in Facebook, Twitter or other social media. To use this library, download jar file and copy it into “libs” directory under  Android project directory.

long now 			= System.currentTimeMillis();
long secondsAgo		= now - 3000;
long minutesAgo		= now - 300000;
long hoursAgo		= now - 3600000;
long monthsAgo		= timeStringtoMilis("2014-01-02 00:00:00");
long longTimeAgo	= timeStringtoMilis("2004-05-12 09:33:12");

PrettyTime prettyTime = new PrettyTime();

textView01.setText(prettyTime.format(new Date(secondsAgo)));
textView02.setText(prettyTime.format(new Date(minutesAgo)));
textView03.setText(prettyTime.format(new Date(hoursAgo)));
textView04.setText(prettyTime.format(new Date(monthsAgo)));
textView05.setText(prettyTime.format(new Date(longTimeAgo)));

The results:

Prettytime Android

Download the source code for this example tutorial

facebooktwittergoogle_plusredditpinterestlinkedinmailby feather

Comments

  1. cycorax12 says

    Hi,

    Can you tell me how to store date and time captured from date and time dialog picker in android in database sqlite. And then again retrieve and compare them with current date

Leave a Reply

Your email address will not be published. Required fields are marked *