-1

Now i am going on with the Android Time Ago i am getting days ago but not getting weeks ago

Here i tried:

CharSequence    CS = DateUtils.getRelativeTimeSpanString(DateUtils.MINUTE_IN_MILLIS, now.getTime(),
                    DateUtils.WEEK_IN_MILLIS, 0);

Exactly what i need is if it less than week should show these much days remaning.

if a week and above shows how many weeks and reaches a month should shows a month ago or two month ago like goes on

How can i get this can anyone help me.

Manoj
  • 3,947
  • 9
  • 46
  • 84
  • Possible duplicate of [How to calculate "time ago" in Java?](https://stackoverflow.com/questions/3859288/how-to-calculate-time-ago-in-java) – Basil Bourque Sep 03 '18 at 06:14
  • Please search Stack Overflow before posting. This has been [addressed many times already](https://duckduckgo.com/?q=site%3Astackoverflow.com+java+time+ago&t=ffab&ia=web). Searching for [the term `relative time`](https://duckduckgo.com/?q=site%3Astackoverflow.com+java+relative+time&t=ffab&ia=web) might also be productive. – Basil Bourque Sep 03 '18 at 06:15

2 Answers2

1

You can use my library (Timeago) for getting exact Timeago for Java/Android, as for displaying past and future datetime values, you can check this example for current time:

long timeInMillis = System.currentTimeMillis();
String text = TimeAgo.using(timeInMillis);

For future dates, you can use the java.util.Calendar class, and setting day value greater than current date, then getting the time as a long type as an input for this class.

Marlon López
  • 460
  • 8
  • 21
0

You can not do that via existing methods of DateUtils. You can use this implementation for that, but keep in mind that it works correct only for past (weeks, month, years). If you want to handle future then you need care about that use case.

public static final long AVERAGE_MONTH_IN_MILLIS = DateUtils.DAY_IN_MILLIS * 30;

private String getRelationTime(long time) {
    final long now = new Date().getTime();
    final long delta = now - time;
    long resolution;
    if (delta <= DateUtils.MINUTE_IN_MILLIS) {
        resolution = DateUtils.SECOND_IN_MILLIS;
    } else if (delta <= DateUtils.HOUR_IN_MILLIS) {
        resolution = DateUtils.MINUTE_IN_MILLIS;
    } else if (delta <= DateUtils.DAY_IN_MILLIS) {
        resolution = DateUtils.HOUR_IN_MILLIS;
    } else if (delta <= DateUtils.WEEK_IN_MILLIS) {
        resolution = DateUtils.DAY_IN_MILLIS;
    } else if (delta <= AVERAGE_MONTH_IN_MILLIS) { 
        return Integer.toString((int) (delta / DateUtils.WEEK_IN_MILLIS)) + " weeks(s) ago";
    } else if (delta <= DateUtils.YEAR_IN_MILLIS) {
        return Integer.toString((int) (delta / AVERAGE_MONTH_IN_MILLIS)) + " month(s) ago";
    } else {
        return Integer.toString((int) (delta / DateUtils.YEAR_IN_MILLIS)) + " year(s) ago";
    }
    return DateUtils.getRelativeTimeSpanString(time, now, resolution).toString();
}
gio
  • 4,950
  • 3
  • 32
  • 46