• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • About
  • Projects
    • GStaticMap WP Plugin
  • Contact
  • Privacy Policy

Lorenz Blog

All About Web & Mobile Application Development

  • Featured Articles
  • Gadgets
    • Android
    • Blackberry
  • Programming
    • Android
    • PHP
    • Java Script
    • MySQL
    • Postgresql
    • Flex
    • Web
  • Software
    • Mac OS
    • Windows
    • Linux
  • Web
You are Here » Home >> Information Technology >> Programming >> Android >> How to Convert Time in Milliseconds into Relative Time Format in Android

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

February 14, 2014 by Lorensius Londa 2 Comments

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

[ad#ad-720×90]

Download the source code for this example tutorial

Facebooktwitterredditpinterestlinkedinmailby feather

Related posts:

  1. How to solve error: Conversion to Dalvik format failed with error 1 in Android on export
  2. How to Create Android Update Zip Package
  3. How to Post Twitter Status from Android
  4. Convert Video with Free Video Converter

Filed Under: Android Tagged With: Android, dateutils, human readable, java, prettytime, time formatting

About Lorensius Londa

Passionate web and mobile application developer. Co-founder of TRUSTUDIO, loves programming, Android, aviation, travelling, photography, coffee and gym mania.

Reader Interactions

Comments

  1. cycorax12 says

    May 5, 2014 at 12:25 pm

    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

    Reply
  2. Madhu says

    February 2, 2015 at 3:44 pm

    Thanx . helped me

    Reply

Leave a Reply Cancel reply

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

Primary Sidebar

About Me

A husband, father of two, passionate software developer, diy lover and home baker who loves to learn new things. Read More…

  • Facebook
  • GitHub
  • Google+
  • Instagram
  • Twitter
  • YouTube

Featured Articles

How to Setup MQTT Server Using Mosquitto and Libwebsocket on Freebsd

Blue Bamboo P25 Printer Android Demo Application With Source Code

Simple JSON RPC Client for Android

How to Send Message to Google Cloud Messaging (GCM) Server Using JSON and PHP

Footer

Recent Comments

  • Aditya Dabas on About
  • Ayten Göksenin Barutçu on How to Make Android Map Scrollable Inside a ScrollView Layout
  • mang jojot on About
  • Hussain on How to Programmatically Scan or Discover Android Bluetooth Devices

Recent Posts

  • How to Fix Blank Screen on WordPress Add/Edit Post Page
  • How to Programmatically Restart the ESP32 Board
  • How to Get Hardware Info of ESP32
  • How to Setup MQTT Server Using Mosquitto and Libwebsocket on Freebsd

Latest Tweets

To protect our users from spam and other malicious activity, this account is temporarily locked. Please log in to https://twitter.com to unlock your account.

Copyright © 2023 · Magazine Pro on Genesis Framework · WordPress · Log in