2

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!

Praneeth
  • 1,260
  • 18
  • 37
  • Consider using JodaTime, [see this previous answer](https://stackoverflow.com/questions/2179644/how-to-calculate-elapsed-time-from-now-with-joda-time) – Ed Holloway-George Feb 28 '17 at 12:42
  • is `startDate` in future? (not past) – Yazan Feb 28 '17 at 12:49
  • @Yazan think of this way, see the time beside your comment it will show 44 sec ago now and later it will change to some thing else, I want like this exactly – Praneeth Feb 28 '17 at 12:51
  • i know what you mean , i am asking if 9for any reason) the startDate is in future, --yet to come-- because based on this answer http://stackoverflow.com/a/15216466/3604083 using minutes as Resolution will show the date, you need to use days as Resolution – Yazan Feb 28 '17 at 12:54
  • @praneethkumar Please have a look on my answer if it is solved your problem then please accept this answer. – Dharmbir Singh Feb 28 '17 at 13:06

1 Answers1

1

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";
        }
    }
}

}

Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66