praneeth kumar praneeth kumar -4 years ago 140
Android Question

How to change string time stamp into Human readable date format?

I'm trying to convert a string time stamp into something like the following:

2 Min ago.
1 Hr ago.
4 Days ago.
3 weeks ago.
5 months ago.
1 year ago.

I'm using Java's

DateUtils.getRelativeTimeSpanString()
method to convert but its is showing date like 1 sep 2016. I want it to show as 6 months ago.

Here is the code :

long now = System.currentTimeMillis();
System.err.println();

DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.ENGLISH);
Date startDate;

try {
startDate = df.parse(userLogs.get(i).getDate());
String sds = (String) DateUtils.getRelativeTimeSpanString(startDate.getTime(),System.currentTimeMillis(),DateUtils.MINUTE_IN_MILLIS);
System.err.println(sds);
} catch (ParseException e) {
e.printStackTrace();
}


Update: I don't want to use Joda time or any other library why it is marked as duplicate I don't understand!

Answer Source

Please try to use this class

 long time = Long.valueOf(getDateFromDateTime("2016-09-01 15:57:20"));//2016-09-01 15:57:20 pass your date here

    String timeStr = TimeUtility.timeAgo(time/1000);

Output: one minute ago

public long getDateFromDateTime(String date) {
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
            Locale.US);
    Date new_date;
    long t = 0L;
    try {
        new_date = df.parse(date);
        t = new_date.getTime();
        return t;
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return 0;
}


public class TimeUtility {

public static String timeAgo(long time_ago) {
    long cur_time = (Calendar.getInstance().getTimeInMillis()) / 1000;
    long time_elapsed = cur_time - time_ago;
    long seconds = time_elapsed;
    int minutes = Math.round(time_elapsed / 60);
    int hours = Math.round(time_elapsed / 3600);
    int days = Math.round(time_elapsed / 86400);
    int weeks = Math.round(time_elapsed / 604800);
    int months = Math.round(time_elapsed / 2600640);
    int years = Math.round(time_elapsed / 31207680);

    // Seconds
    if (seconds <= 60) {
        return "just now";
    }
    //Minutes
    else if (minutes <= 60) {
        if (minutes == 1) {
            return "one minute ago";
        } else {
            return minutes + " minutes ago";
        }
    }
    //Hours
    else if (hours <= 24) {
        if (hours == 1) {
            return "an hour ago";
        } else {
            return hours + " hrs ago";
        }
    }
    //Days
    else if (days <= 7) {
        if (days == 1) {
            return "yesterday";
        } else {
            return days + " days ago";
        }
    }
    //Weeks
    else if (weeks <= 4.3) {
        if (weeks == 1) {
            return "a week ago";
        } else {
            return weeks + " weeks ago";
        }
    }
    //Months
    else if (months <= 12) {
        if (months == 1) {
            return "a month ago";
        } else {
            return months + " months ago";
        }
    }
    //Years
    else {
        if (years == 1) {
            return "one year ago";
        } else {
            return years + " years ago";
        }
    }
}

}

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download