How to convert time to " time ago " in android How to convert time to " time ago " in android android android

How to convert time to " time ago " in android


I see mainly three ways:

a) built-in options using SimpleDateFormat and DateUtils

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");  sdf.setTimeZone(TimeZone.getTimeZone("GMT"));  try {         long time = sdf.parse("2016-01-24T16:00:00.000Z").getTime();         long now = System.currentTimeMillis();         CharSequence ago =                    DateUtils.getRelativeTimeSpanString(time, now, DateUtils.MINUTE_IN_MILLIS);        } catch (ParseException e) {            e.printStackTrace();        }

b) external library ocpsoft/PrettyTime (based on java.util.Date)

Here you have to use SimpleDateFormat, too, to produce the time-result as interpretation of "2016-01-24T16:00:00.000Z".

import below lib in your app

implementation 'org.ocpsoft.prettytime:prettytime:4.0.1.Final'

PrettyTime prettyTime = new PrettyTime(Locale.getDefault());String ago = prettyTime.format(new Date(time));

c) using my library Time4A (heavyweight but with best i18n-support)

Moment moment = Iso8601Format.EXTENDED_DATE_TIME_OFFSET.parse("2016-01-24T16:00:00.000Z");String ago = PrettyTime.of(Locale.getDefault()).printRelativeInStdTimezone(moment);


1 - create date formatter:

public static final SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

2 - create Date object

String dateStr = "2016-01-24T16:00:00.000Z";Date date = inputFormat.parse(dateStr);

3 - Use Android DateUtils to create a nice Display string:

String niceDateStr = DateUtils.getRelativeTimeSpanString(date.getTime() , Calendar.getInstance().getTimeInMillis(), DateUtils.MINUTE_IN_MILLIS);


It's very simple. I'll tell you with my code.

package com.example;import java.text.SimpleDateFormat;import java.util.Date;import java.util.concurrent.TimeUnit;public class TimeShow{    public static void main(String[] args) {        try         {            SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss");            Date past = format.parse("2016.02.05 AD at 23:59:30");            Date now = new Date();            long seconds=TimeUnit.MILLISECONDS.toSeconds(now.getTime() - past.getTime());            long minutes=TimeUnit.MILLISECONDS.toMinutes(now.getTime() - past.getTime());            long hours=TimeUnit.MILLISECONDS.toHours(now.getTime() - past.getTime());            long days=TimeUnit.MILLISECONDS.toDays(now.getTime() - past.getTime());////          System.out.println(TimeUnit.MILLISECONDS.toSeconds(now.getTime() - past.getTime()) + " milliseconds ago");//          System.out.println(TimeUnit.MILLISECONDS.toMinutes(now.getTime() - past.getTime()) + " minutes ago");//          System.out.println(TimeUnit.MILLISECONDS.toHours(now.getTime() - past.getTime()) + " hours ago");//          System.out.println(TimeUnit.MILLISECONDS.toDays(now.getTime() - past.getTime()) + " days ago");            if(seconds<60)            {                System.out.println(seconds+" seconds ago");            }            else if(minutes<60)            {                System.out.println(minutes+" minutes ago");            }            else if(hours<24)            {                System.out.println(hours+" hours ago");            }            else             {                System.out.println(days+" days ago");            }        }        catch (Exception j){            j.printStackTrace();        }    }}