How to convert time to " time ago " in android

AndroidDatetimeTimeCalendar

Android Problem Overview


My server. It return time :

"2016-01-24T16:00:00.000Z"

I want

1 : convert to String.

2 : I want it show " time ago " when load it from server.

Please. Help me!

Android Solutions


Solution 1 - 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);

Solution 2 - Android

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);

Solution 3 - Android

using @Excelso_Widi code, i was able to overcome,

I modified his code and also translated to English.

public class TimeAgo2 {

    public String covertTimeToText(String dataDate) {

        String convTime = null;

        String prefix = "";
        String suffix = "Ago";

        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
            Date pasTime = dateFormat.parse(dataDate);

            Date nowTime = new Date();

            long dateDiff = nowTime.getTime() - pasTime.getTime();

            long second = TimeUnit.MILLISECONDS.toSeconds(dateDiff);
            long minute = TimeUnit.MILLISECONDS.toMinutes(dateDiff);
            long hour   = TimeUnit.MILLISECONDS.toHours(dateDiff);
            long day  = TimeUnit.MILLISECONDS.toDays(dateDiff);

            if (second < 60) {
                convTime = second + " Seconds " + suffix;
            } else if (minute < 60) {
                convTime = minute + " Minutes "+suffix;
            } else if (hour < 24) {
                convTime = hour + " Hours "+suffix;
            } else if (day >= 7) {
                if (day > 360) {
                    convTime = (day / 360) + " Years " + suffix;
                } else if (day > 30) {
                    convTime = (day / 30) + " Months " + suffix;
                } else {
                    convTime = (day / 7) + " Week " + suffix;
                }
            } else if (day < 7) {
                convTime = day+" Days "+suffix;
            }

        } catch (ParseException e) {
            e.printStackTrace();
            Log.e("ConvTimeE", e.getMessage());
        }

        return convTime;
    }

}

and i used it like this

 String time = jsonObject.getString("date_gmt");
 TimeAgo2 timeAgo2 = new TimeAgo2();
String MyFinalValue = timeAgo2.covertTimeToText(time);

Happy coding and thanks @Excelso_Widi you the man wink

Solution 4 - Android

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();
		}
	}
}

Solution 5 - Android

In Android you can use DateUtils.getRelativeTimeSpanString(long timeInMillis), referring to https://developer.android.com/reference/android/text/format/DateUtils.html you can use one of the variations of that method for accuracy.

Solution 6 - Android

Kotlin version

private const val SECOND_MILLIS = 1000
private const val MINUTE_MILLIS = 60 * SECOND_MILLIS
private const val HOUR_MILLIS = 60 * MINUTE_MILLIS
private const val DAY_MILLIS = 24 * HOUR_MILLIS

private fun currentDate(): Date {
    val calendar = Calendar.getInstance()
    return calendar.time
}

fun getTimeAgo(date: Date): String {
    var time = date.time
    if (time < 1000000000000L) {
        time *= 1000
    }

    val now = currentDate().time
    if (time > now || time <= 0) {
        return "in the future"
    }

    val diff = now - time
    return when {
        diff < MINUTE_MILLIS -> "moments ago"
        diff < 2 * MINUTE_MILLIS -> "a minute ago"
        diff < 60 * MINUTE_MILLIS -> "${diff / MINUTE_MILLIS} minutes ago"
        diff < 2 * HOUR_MILLIS -> "an hour ago"
        diff < 24 * HOUR_MILLIS -> "${diff / HOUR_MILLIS} hours ago"
        diff < 48 * HOUR_MILLIS -> "yesterday"
        else -> "${diff / DAY_MILLIS} days ago"
    }
}

Solution 7 - Android

For kotlin, you can use this extension function.

fun Date.getTimeAgo(): String {
    val calendar = Calendar.getInstance()
    calendar.time = this

    val year = calendar.get(Calendar.YEAR)
    val month = calendar.get(Calendar.MONTH)
    val day = calendar.get(Calendar.DAY_OF_MONTH)
    val hour = calendar.get(Calendar.HOUR_OF_DAY)
    val minute = calendar.get(Calendar.MINUTE)

    val currentCalendar = Calendar.getInstance()

    val currentYear = currentCalendar.get(Calendar.YEAR)
    val currentMonth = currentCalendar.get(Calendar.MONTH)
    val currentDay = currentCalendar.get(Calendar.DAY_OF_MONTH)
    val currentHour = currentCalendar.get(Calendar.HOUR_OF_DAY)
    val currentMinute = currentCalendar.get(Calendar.MINUTE)

    return if (year < currentYear ) {
        val interval = currentYear - year
        if (interval == 1) "$interval year ago" else "$interval years ago"
    } else if (month < currentMonth) {
        val interval = currentMonth - month
        if (interval == 1) "$interval month ago" else "$interval months ago"
    } else  if (day < currentDay) {
        val interval = currentDay - day
        if (interval == 1) "$interval day ago" else "$interval days ago"
    } else if (hour < currentHour) {
        val interval = currentHour - hour
        if (interval == 1) "$interval hour ago" else "$interval hours ago"
    } else if (minute < currentMinute) {
        val interval = currentMinute - minute
        if (interval == 1) "$interval minute ago" else "$interval minutes ago"
    } else {
        "a moment ago"
    }
}

// To use it
val timeAgo = someDate.getTimeAgo()

Solution 8 - Android

private static final int SECOND_MILLIS = 1000;
private static final int MINUTE_MILLIS = 60 * SECOND_MILLIS;
private static final int HOUR_MILLIS = 60 * MINUTE_MILLIS;
private static final int DAY_MILLIS = 24 * HOUR_MILLIS;

private static Date currentDate() {
    Calendar calendar = Calendar.getInstance();
    return calendar.getTime();
}

public static String getTimeAgo(Date date) {
    long time = date.getTime();
    if (time < 1000000000000L) {
        time *= 1000;
    }

    long now = currentDate().getTime();
    if (time > now || time <= 0) {
        return "in the future";
    }

    final long diff = now - time;
    if (diff < MINUTE_MILLIS) {
        return "moments ago";
    } else if (diff < 2 * MINUTE_MILLIS) {
        return "a minute ago";
    } else if (diff < 60 * MINUTE_MILLIS) {
        return diff / MINUTE_MILLIS + " minutes ago";
    } else if (diff < 2 * HOUR_MILLIS) {
        return "an hour ago";
    } else if (diff < 24 * HOUR_MILLIS) {
        return diff / HOUR_MILLIS + " hours ago";
    } else if (diff < 48 * HOUR_MILLIS) {
        return "yesterday";
    } else {
        return diff / DAY_MILLIS + " days ago";
    }
}

just call getTimeAgo(timeInDate);

It's working for me.

Solution 9 - Android

Step 1. Convert your time string to milisecond format with long type

Step 2. Use code bellow

private static final int SECOND_MILLIS = 1000; 
private static final int MINUTE_MILLIS = 60 * SECOND_MILLIS; 
private static final int HOUR_MILLIS = 60 * MINUTE_MILLIS; 
private static final int DAY_MILLIS = 24 * HOUR_MILLIS; 

public static String getTimeAgo(long time, Context ctx) { 
    if (time < 1000000000000L) { 
        //if timestamp given in seconds, convert to millis time *= 1000; 
    } 

    long now = getCurrentTime(ctx); 
    if (time > now || time <= 0) { 
        return null; 
    } 

    // TODO: localize 
    
    final long diff = now - time; 
    
    if (diff < MINUTE_MILLIS) { return "just now"; } 
    else if (diff < 2 * MINUTE_MILLIS) { return "a minute ago"; } 
    else if (diff < 50 * MINUTE_MILLIS) { return diff / MINUTE_MILLIS + " minutes ago"; } 
    else if (diff < 90 * MINUTE_MILLIS) { return "an hour ago"; } 
    else if (diff < 24 * HOUR_MILLIS) { return diff / HOUR_MILLIS + " hours ago"; } else if (diff < 48 * HOUR_MILLIS) { return "yesterday"; } 
    else { return diff / DAY_MILLIS + " days ago"; } 
}

//above code from google !

Solution 10 - Android

You can pass milliseconds in getlongtoago method and you get return perfect formatted string

public static String getlongtoago(long createdAt) {
    DateFormat userDateFormat = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
    DateFormat dateFormatNeeded = new SimpleDateFormat("MM/dd/yyyy HH:MM:SS");
    Date date = null;
    date = new Date(createdAt);
    String crdate1 = dateFormatNeeded.format(date);

    // Date Calculation
    DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
    crdate1 = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(date);

    // get current date time with Calendar()
    Calendar cal = Calendar.getInstance();
    String currenttime = dateFormat.format(cal.getTime());

    Date CreatedAt = null;
    Date current = null;
    try {
        CreatedAt = dateFormat.parse(crdate1);
        current = dateFormat.parse(currenttime);
    } catch (java.text.ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // Get msec from each, and subtract.
    long diff = current.getTime() - CreatedAt.getTime();
    long diffSeconds = diff / 1000;
    long diffMinutes = diff / (60 * 1000) % 60;
    long diffHours = diff / (60 * 60 * 1000) % 24;
    long diffDays = diff / (24 * 60 * 60 * 1000);

    String time = null;
    if (diffDays > 0) {
        if (diffDays == 1) {
            time = diffDays + "day ago ";
        } else {
            time = diffDays + "days ago ";
        }
    } else {
        if (diffHours > 0) {
            if (diffHours == 1) {
                time = diffHours + "hr ago";
            } else {
                time = diffHours + "hrs ago";
            }
        } else {
            if (diffMinutes > 0) {
                if (diffMinutes == 1) {
                    time = diffMinutes + "min ago";
                } else {
                    time = diffMinutes + "mins ago";
                }
            } else {
                if (diffSeconds > 0) {
                    time = diffSeconds + "secs ago";
                }
            }

        }

    }
    return time;
}

Solution 11 - Android

public class TimeUtility {

public String covertTimeToText(String dataDate) {

    String convTime = null;

    try {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date pasTime = dateFormat.parse(dataDate);

        Date nowTime = new Date();

        long dateDiff = nowTime.getTime() - pasTime.getTime();

        long detik = TimeUnit.MILLISECONDS.toSeconds(dateDiff);
        long menit = TimeUnit.MILLISECONDS.toMinutes(dateDiff);
        long jam   = TimeUnit.MILLISECONDS.toHours(dateDiff);
        long hari  = TimeUnit.MILLISECONDS.toDays(dateDiff);

        if (detik < 60) {
            convTime = detik+"detik lalu";
        } else if (menit < 60) {
            convTime = menit+"menit lalu";
        } else if (jam < 24) {
            convTime = jam+"jam lalu";
        } else if (hari >= 7) {
            if (hari > 30) {
                convTime = (hari / 30)+"bulan lalu";
            } else if (hari > 360) {
                convTime = (hari / 360)+"tahun lalu";
            } else {
                convTime = (hari / 7) + "minggu lalu";
            }
        } else if (hari < 7) {
            convTime = hari+"hari lalu";
        }

    } catch (ParseException e) {
        e.printStackTrace();
        Log.e("ConvTimeE", e.getMessage());
    }

    return convTime;

  }

}

Solution 12 - Android

You can select what type format you want both method are tested and working fine.

/*
* It's return date  before one week timestamp
*  like return
*  1 day ago
*  2 days ago
*  5 days ago
*  21 April 2019
*
* */
public static String getTimeAgoDate(long pastTimeStamp) {

    // for 2 min ago   use  DateUtils.MINUTE_IN_MILLIS
    // for 2 sec ago   use  DateUtils.SECOND_IN_MILLIS
    // for 1 hours ago use  DateUtils.HOUR_IN_MILLIS

    long now = System.currentTimeMillis();

    if (now - pastTimeStamp < 1000) {
        pastTimeStamp = pastTimeStamp + 1000;
    }
    CharSequence ago =
            DateUtils.getRelativeTimeSpanString(pastTimeStamp, now, DateUtils.SECOND_IN_MILLIS);
    return ago.toString();
}


/*
 *
 * It's return date  before one week timestamp
 *
 *  like return
 *
 *  1 day ago
 *  2 days ago
 *  5 days ago
 *  2 weeks ago
 *  2 months ago
 *  2 years ago
 *
 *
 * */

public static String getTimeAgo(long mReferenceTime) {

    long now = System.currentTimeMillis();
    final long diff = now - mReferenceTime;
    if (diff < android.text.format.DateUtils.WEEK_IN_MILLIS) {
        return (diff <= 1000) ?
                "just now" :
                android.text.format.DateUtils.getRelativeTimeSpanString(mReferenceTime, now, DateUtils.MINUTE_IN_MILLIS,
                        DateUtils.FORMAT_ABBREV_RELATIVE).toString();
    } else if (diff <= 4 * android.text.format.DateUtils.WEEK_IN_MILLIS) {
        int week = (int)(diff / (android.text.format.DateUtils.WEEK_IN_MILLIS));
        return  week>1?week+" weeks ago":week+" week ago";
    } else if (diff < android.text.format.DateUtils.YEAR_IN_MILLIS) {
        int month = (int)(diff / (4 * android.text.format.DateUtils.WEEK_IN_MILLIS));
        return  month>1?month+" months ago":month+" month ago";
    } else {
        int year = (int) (diff/DateUtils.YEAR_IN_MILLIS);
        return year>1?year+" years ago":year+" year ago";
    }
}

Thanks

Solution 13 - Android

Please check the code below to do this perfectly in a modular and reusable way which will also return you the future time as like 5 minutes later also. First, you need to create the UnixToHuman class in your project I mention below. Then you need to convert the server returned string into UNIX timestamp by

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
long time = sdf.parse("2016-01-24T16:00:00.000Z").getTime();

Then you need to just call the function UnixToHuman.getTimeAgo(long time) where time is your UNIX time which will return the desired string. The UnixToHuman.java is

public class UnixToHuman {
    private static final int SECOND_MILLIS = 1000;
    private static final int MINUTE_MILLIS = 60 * SECOND_MILLIS;
    private static final int HOUR_MILLIS = 60 * MINUTE_MILLIS;
    private static final int DAY_MILLIS = 24 * HOUR_MILLIS;
    private static final int WEEK_MILLIS = 7 * DAY_MILLIS ;

    public static String getTimeAgo(long time) {
        if (time < 1000000000000L) {
            // if timestamp given in seconds, convert to millis
            time *= 1000;
        }

        long now =System.currentTimeMillis();;

        
        long diff = now - time;
        if(diff>0) {

           
            if (diff < MINUTE_MILLIS) {
                return "just now";
            } else if (diff < 2 * MINUTE_MILLIS) {
                return "a minute ago";
            } else if (diff < 50 * MINUTE_MILLIS) {
                return diff / MINUTE_MILLIS + " minutes ago";
            } else if (diff < 90 * MINUTE_MILLIS) {
                return "an hour ago";
            } else if (diff < 24 * HOUR_MILLIS) {
                return diff / HOUR_MILLIS + " hours ago";
            } else if (diff < 48 * HOUR_MILLIS) {
                return "yesterday";
            } else if (diff < 7 * DAY_MILLIS) {
                return diff / DAY_MILLIS + " days ago";
            } else if (diff < 2 * WEEK_MILLIS) {
                return "a week ago";
            } else if (diff < WEEK_MILLIS * 3) {
                return diff / WEEK_MILLIS + " weeks ago";
            } else {
                java.util.Date date = new java.util.Date((long) time);
                return date.toString();
            }

        }
        else {
           
            diff=time-now;
            if (diff < MINUTE_MILLIS) {
                return "this minute";
            } else if (diff < 2 * MINUTE_MILLIS) {
                return "a minute later";
            } else if (diff < 50 * MINUTE_MILLIS) {
                return diff / MINUTE_MILLIS + " minutes later";
            } else if (diff < 90 * MINUTE_MILLIS) {
                return "an hour later";
            } else if (diff < 24 * HOUR_MILLIS) {
                return diff / HOUR_MILLIS + " hours later";
            } else if (diff < 48 * HOUR_MILLIS) {
                return "tomorrow";
            } else if (diff < 7 * DAY_MILLIS) {
                return diff / DAY_MILLIS + " days later";
            } else if (diff < 2 * WEEK_MILLIS) {
                return "a week later";
            } else if (diff < WEEK_MILLIS * 3) {
                return diff / WEEK_MILLIS + " weeks later";
            } else {
                java.util.Date date = new java.util.Date((long) time);
                return date.toString();
            }
        }

    }
}

Solution 14 - Android

What you are trying to convert is ISO 8601 compliant format. The easiest way to convert this is by using Joda-Time library for Android.

Once you add that to your project, you can use this code to extract the exact date!

    DateTimeZone timeZone = DateTimeZone.getDefault();
    DateTimeFormatter formatter = DateTimeFormat.forPattern("dd MMMM yyyy").withZone(timeZone);
    DateTime dateTime2 = new DateTime( isoDateToBeConverted, timeZone );
    String output = formatter.print( dateTime2 );
    Log.w("TIME IF WORKS::",""+output);

Also, see this to format the date in your preferred choice Hope it helps!

Solution 15 - Android

Modified Above Answer:

public class TimeAgo {

 

   public String covertTimeToText(String dataDate) {

        String convertTime = null;
    String suffix = "ago";

    try {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        Date pasTime = dateFormat.parse(dataDate);

        Date nowTime = new Date();

        long dateDiff = nowTime.getTime() - pasTime.getTime();

        long second = TimeUnit.MILLISECONDS.toSeconds(dateDiff);
        long minute = TimeUnit.MILLISECONDS.toMinutes(dateDiff);
        long hour = TimeUnit.MILLISECONDS.toHours(dateDiff);
        long day = TimeUnit.MILLISECONDS.toDays(dateDiff);

        if (second < 60) {
            if (second == 1) {
                convertTime = second + " second " + suffix;
            } else {
                convertTime = second + " seconds " + suffix;
            }
        } else if (minute < 60) {
            if (minute == 1) {
                convertTime = minute + " minute " + suffix;
            } else {
                convertTime = minute + " minutes " + suffix;
            }
        } else if (hour < 24) {
            if (hour == 1) {
                convertTime = hour + " hour " + suffix;
            } else {
                convertTime = hour + " hours " + suffix;
            }
        } else if (day >= 7) {
            if (day >= 365) {
                long tempYear = day / 365;
                if (tempYear == 1) {
                    convertTime = tempYear + " year " + suffix;
                } else {
                    convertTime = tempYear + " years " + suffix;
                }
            } else if (day >= 30) {
                long tempMonth = day / 30;
                if (tempMonth == 1) {
                    convertTime = (day / 30) + " month " + suffix;
                } else {
                    convertTime = (day / 30) + " months " + suffix;
                }
            } else {
                long tempWeek = day / 7;
                if (tempWeek == 1) {
                    convertTime = (day / 7) + " week " + suffix;
                } else {
                    convertTime = (day / 7) + " weeks " + suffix;
                }
            }
        } else {
            if (day == 1) {
                convertTime = day + " day " + suffix;
            } else {
                convertTime = day + " days " + suffix;
            }
        }

    } catch (ParseException e) {
        e.printStackTrace();
        Log.e("TimeAgo", e.getMessage() + "");
    }
    return convertTime;
   }

}

Solution 16 - Android

As I went through all the answers, there are a bunch of ways to get the solution for this query anyhow the below answer does not require any third-party modules or any native util classes, you can get the result from native Java using the below technique also it's a memory-optimized answer, it won't consume your memory much.

Usage:

HumanDateUtils.durationFromNow(startDate)

You can customize this method as your wish by adding ago or seen.

import java.util.Date;

public class HumanDateUtils {

    public static String durationFromNow(Date startDate) {

        long different = System.currentTimeMillis() - startDate.getTime();

        long secondsInMilli = 1000;
        long minutesInMilli = secondsInMilli * 60;
        long hoursInMilli = minutesInMilli * 60;
        long daysInMilli = hoursInMilli * 24;

        long elapsedDays = different / daysInMilli;
        different = different % daysInMilli;

        long elapsedHours = different / hoursInMilli;
        different = different % hoursInMilli;

        long elapsedMinutes = different / minutesInMilli;
        different = different % minutesInMilli;

        long elapsedSeconds = different / secondsInMilli;

        String output = "";
        if (elapsedDays > 0) output += elapsedDays + "days ";
        if (elapsedDays > 0 || elapsedHours > 0) output += elapsedHours + " hours ";
        if (elapsedHours > 0 || elapsedMinutes > 0) output += elapsedMinutes + " minutes ";
        if (elapsedMinutes > 0 || elapsedSeconds > 0) output += elapsedSeconds + " seconds";

        return output;
    }
}

Output:
12 days 12 hours 25 minutes 4 seconds

Reference: Human Readable Date - Java

Solution 17 - Android

java.time

The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.

Also, quoted below is a notice from the home page of Joda-Time:

> Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) - a core part of the JDK which replaces this project.

Solution using java.time, the modern Date-Time API: You can use java.time.Duration which was introduced with Java-8 as part of JSR-310 implementation to model ISO_8601#Duration. With Java-9 some more convenience methods were introduced.

Demo:

import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.TimeUnit;

public class Main {
	public static void main(String args[]) {
		String strDateTime = "2016-01-24T16:00:00.000Z";
		Instant then = Instant.parse(strDateTime);
		Instant now = Instant.now();
		Duration duration = Duration.between(then, now);
		System.out.println(duration);

		// ####################################Java-8####################################
		String formatted = String.format("%d days %02d hours %02d minutes %02d seconds %02d milliseconds ago",
				duration.toDays(), duration.toHours() % 24, duration.toMinutes() % 60, duration.toSeconds() % 60,
				TimeUnit.MILLISECONDS.convert(duration.toNanos() % 1000_000_000, TimeUnit.NANOSECONDS));
		System.out.println(formatted);
		// ##############################################################################

		// ####################################Java-9####################################
		formatted = String.format("%d days %02d hours %02d minutes %02d seconds %02d milliseconds ago",
				duration.toDaysPart(), duration.toHoursPart(), duration.toMinutesPart(), duration.toSecondsPart(),
				TimeUnit.MILLISECONDS.convert(duration.toNanosPart(), TimeUnit.NANOSECONDS));
		System.out.println(formatted);
		// ####################################Java-9####################################
	}
}

Output:

PT50117H11M53.914442S
2088 days 05 hours 11 minutes 53 seconds 914 milliseconds ago
2088 days 05 hours 11 minutes 53 seconds 914 milliseconds ago

ONLINE DEMO

Learn more about the modern Date-Time API* from Trail: Date Time.


* If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring. Note that Android 8.0 Oreo already provides support for java.time.

Solution 18 - Android

Easiest way

For Kotlin (time in millisecond)

private const val SECOND = 1
private const val MINUTE = 60 * SECOND
private const val HOUR = 60 * MINUTE
private const val DAY = 24 * HOUR
private const val MONTH = 30 * DAY
private const val YEAR = 12 * MONTH

private fun currentDate(): Long {
    val calendar = Calendar.getInstance()
    return calendar.timeInMillis
}

// Long: time in millisecond
fun Long.toTimeAgo(): String {
    val time = this
    val now = currentDate()

    // convert back to second
    val diff = (now - time) / 1000

    return when {
        diff < MINUTE -> "Just now"
        diff < 2 * MINUTE -> "a minute ago"
        diff < 60 * MINUTE -> "${diff / MINUTE} minutes ago"
        diff < 2 * HOUR -> "an hour ago"
        diff < 24 * HOUR -> "${diff / HOUR} hours ago"
        diff < 2 * DAY -> "yesterday"
        diff < 30 * DAY -> "${diff / DAY} days ago"
        diff < 2 * MONTH -> "a month ago"
        diff < 12 * MONTH -> "${diff / MONTH} months ago"
        diff < 2 * YEAR -> "a year ago"
        else -> "${diff / YEAR} years ago"
    }
}

Solution 19 - Android

#Try this# I was facing same issue(Unhandled execption java.text.ParseExecption) when trying to convert timestamp into time ago format ,after doing R&D finally i got the solutions...now this error have been solved

  • Add this(compile 'org.ocpsoft.prettytime:prettytime:4.0.1.Final') dependency in your bulid.gradle(Module:app) and sync your project

  • After adding this dependency paste this method and call it where you want like (Log.e("TAG", "ConvertTimeStampintoAgo: "+ConvertTimeStampintoAgo(1320917972));)

     public static String ConvertTimeStampintoAgo(Long timeStamp)
     {
     try
     {
         Calendar cal = Calendar.getInstance(Locale.getDefault());
         cal.setTimeInMillis(timeStamp);
         String date = android.text.format.DateFormat.format("yyyy-MM-dd HH:mm:ss", cal).toString();
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
         Date dateObj = sdf.parse(date);
         PrettyTime p = new PrettyTime();
         return p.format(dateObj);
     }
     catch (Exception e)
     {
         e.printStackTrace();
     }
     return "";
     }
    

Note it will show

  • moment ago(current time)

  • minute ago

  • day ago

Solution 20 - Android

too late but try this,

 public static String parseDate(String givenDateString) {
    if (givenDateString.equalsIgnoreCase("")) {
        return "";
    }

    long timeInMilliseconds=0;
    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
    try {

        Date mDate = sdf.parse(givenDateString);
        timeInMilliseconds = mDate.getTime();
        System.out.println("Date in milli :: " + timeInMilliseconds);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    
    String result = "now";
    SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
    
    String todayDate = formatter.format(new Date());
    Calendar calendar = Calendar.getInstance();

    long dayagolong =  timeInMilliseconds;
    calendar.setTimeInMillis(dayagolong);
    String agoformater = formatter.format(calendar.getTime());

    Date CurrentDate = null;
    Date CreateDate = null;

    try {
        CurrentDate = formatter.parse(todayDate);
        CreateDate = formatter.parse(agoformater);

        long different = Math.abs(CurrentDate.getTime() - CreateDate.getTime());

        long secondsInMilli = 1000;
        long minutesInMilli = secondsInMilli * 60;
        long hoursInMilli = minutesInMilli * 60;
        long daysInMilli = hoursInMilli * 24;

        long elapsedDays = different / daysInMilli;
        different = different % daysInMilli;

        long elapsedHours = different / hoursInMilli;
        different = different % hoursInMilli;

        long elapsedMinutes = different / minutesInMilli;
        different = different % minutesInMilli;

        long elapsedSeconds = different / secondsInMilli;

        different = different % secondsInMilli;
        if (elapsedDays == 0) {
            if (elapsedHours == 0) {
                if (elapsedMinutes == 0) {
                    if (elapsedSeconds < 0) {
                        return "0" + " s";
                    } else {
                        if (elapsedDays > 0 && elapsedSeconds < 59) {
                            return "now";
                        }
                    }
                } else {
                    return String.valueOf(elapsedMinutes) + "mins ago";
                }
            } else {
                return String.valueOf(elapsedHours) + "hr ago";
            }

        } else {
            if (elapsedDays <= 29) {
                return String.valueOf(elapsedDays) + "d ago";
                 
            }
            else if (elapsedDays > 29 && elapsedDays <= 58) {
                return "1Mth ago";
            }
            if (elapsedDays > 58 && elapsedDays <= 87) {
                return "2Mth ago";
            }
            if (elapsedDays > 87 && elapsedDays <= 116) {
                return "3Mth ago";
            }
            if (elapsedDays > 116 && elapsedDays <= 145) {
                return "4Mth ago";
            }
            if (elapsedDays > 145 && elapsedDays <= 174) {
                return "5Mth ago";
            }
            if (elapsedDays > 174 && elapsedDays <= 203) {
                return "6Mth ago";
            }
            if (elapsedDays > 203 && elapsedDays <= 232) {
                return "7Mth ago";
            }
            if (elapsedDays > 232 && elapsedDays <= 261) {
                return "8Mth ago";
            }
            if (elapsedDays > 261 && elapsedDays <= 290) {
                return "9Mth ago";
            }
            if (elapsedDays > 290 && elapsedDays <= 319) {
                return "10Mth ago";
            }
            if (elapsedDays > 319 && elapsedDays <= 348) {
                return "11Mth ago";
            }
            if (elapsedDays > 348 && elapsedDays <= 360) {
                return "12Mth ago";
            }

            if (elapsedDays > 360 && elapsedDays <= 720) {
                return "1 year ago";
            }
        }

    } catch (java.text.ParseException e) {
        e.printStackTrace();
    }
    return result;
}

Solution 21 - Android

As a Kotlin extension function (Replace App.context with your own context):

fun Long.msToTimeAgo(): String {
    val seconds = (System.currentTimeMillis() - this) / 1000f

    return when (true) {
        seconds < 60 -> App.context.resources.getQuantityString(R.plurals.seconds_ago, seconds.toInt(), seconds.toInt())
        seconds < 3600 -> {
            val minutes = seconds / 60f
            App.context.resources.getQuantityString(R.plurals.minutes_ago, minutes.toInt(), minutes.toInt())
        }
        seconds < 86400 -> {
            val hours = seconds / 3600f
            App.context.resources.getQuantityString(R.plurals.hours_ago, hours.toInt(), hours.toInt())
        }
        seconds < 604800 -> {
            val days = seconds / 86400f
            App.context.resources.getQuantityString(R.plurals.days_ago, days.toInt(), days.toInt())
        }
        seconds < 2_628_000 -> {
            val weeks = seconds / 604800f
            App.context.resources.getQuantityString(R.plurals.weeks_ago, weeks.toInt(), weeks.toInt())
        }
        seconds < 31_536_000 -> {
            val months = seconds / 2_628_000f
            App.context.resources.getQuantityString(R.plurals.months_ago, months.toInt(), months.toInt())
        }
        else -> {
            val years = seconds / 31_536_000f
            App.context.resources.getQuantityString(R.plurals.years_ago, years.toInt(), years.toInt())
        }
    }
}

Add the following to your string resources:

<plurals name="seconds_ago">
    <item quantity="one">%d second ago</item>
    <item quantity="other">%d seconds ago</item>
</plurals>

<plurals name="minutes_ago">
    <item quantity="one">%d minute ago</item>
    <item quantity="other">%d minutes ago</item>
</plurals>

<plurals name="hours_ago">
    <item quantity="one">%d hour ago</item>
    <item quantity="other">%d hours ago</item>
</plurals>

<plurals name="days_ago">
    <item quantity="one">%d day ago</item>
    <item quantity="other">%d days ago</item>
</plurals>

<plurals name="weeks_ago">
    <item quantity="one">%d week ago</item>
    <item quantity="other">%d weeks ago</item>
</plurals>

<plurals name="months_ago">
    <item quantity="one">%d month ago</item>
    <item quantity="other">%d months ago</item>
</plurals>

<plurals name="years_ago">
    <item quantity="one">%d year ago</item>
    <item quantity="other">%d years ago</item>
</plurals>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionanh thang BuiView Question on Stackoverflow
Solution 1 - AndroidMeno HochschildView Answer on Stackoverflow
Solution 2 - AndroidAsaf PinhassiView Answer on Stackoverflow
Solution 3 - AndroidThe Billionaire GuyView Answer on Stackoverflow
Solution 4 - AndroidDäñish ShärmàView Answer on Stackoverflow
Solution 5 - AndroidMarlon LópezView Answer on Stackoverflow
Solution 6 - AndroidKavin VarnanView Answer on Stackoverflow
Solution 7 - Androidelbert rivasView Answer on Stackoverflow
Solution 8 - AndroidKiranView Answer on Stackoverflow
Solution 9 - AndroidGiapLeeView Answer on Stackoverflow
Solution 10 - AndroidPankaj TalaviyaView Answer on Stackoverflow
Solution 11 - AndroidWidi Widayat YView Answer on Stackoverflow
Solution 12 - AndroidYogendraView Answer on Stackoverflow
Solution 13 - AndroidGk Mohammad EmonView Answer on Stackoverflow
Solution 14 - AndroidOBXView Answer on Stackoverflow
Solution 15 - AndroidNikesh NayakView Answer on Stackoverflow
Solution 16 - AndroidGooglianView Answer on Stackoverflow
Solution 17 - AndroidArvind Kumar AvinashView Answer on Stackoverflow
Solution 18 - AndroidjorieitomukeView Answer on Stackoverflow
Solution 19 - AndroidSunilView Answer on Stackoverflow
Solution 20 - AndroidAdilView Answer on Stackoverflow
Solution 21 - AndroidJohannView Answer on Stackoverflow