How to Convert Time to " Time Ago " in Android

Date Time Ago Converter

Use this class for Time ago

public class TimeAgo {
public final static String monthAgo = " Month z";
public final static String monthsAgo = " Months ago";

public final static String weekAgo = " Week ago";
public final static String weeksAgo = " Weeks ago";

public final static String daysAgo = " Days ago";
public final static String dayAgo = " Day ago";

public final static String hourAgo = " Hour ago";
public final static String hoursAgo = " Hours ago";

public final static String minAgo = " Min ago";
public final static String minsAgo = " Mins ago";

public final static String secsAgo = " Secs ago";
public final static String secAgo = "Just Now";
static int second = 1000; // milliseconds
static int minute = 60;
static int hour = minute * 60;
static int day = hour * 24;
static int week = day * 7;
static int month = day * 30;
static int year = month * 12;

public static String DateDifference(long fromDate) {
long diff = 0;
long ms2 = System.currentTimeMillis();
// get difference in milli seconds
diff = ms2 - fromDate;

int diffInSec = Math.abs((int) (diff / (second)));
String difference = "";
if (diffInSec < minute) {
if (diffInSec <= 1) {

difference = secAgo;

} else {
difference = diffInSec + secsAgo;
}
} else if ((diffInSec / hour) < 1) {
if ((diffInSec / minute) <= 1) {
difference = (diffInSec / minute) + minAgo;
} else {
difference = (diffInSec / minute) + minsAgo;
}
} else if ((diffInSec / day) < 1) {
if ((diffInSec / hour) <= 1) {
difference = (diffInSec / hour) + hourAgo;
} else {
difference = (diffInSec / hour) + hoursAgo;
}

} else if ((diffInSec / week) < 1) {
if ((diffInSec / day) <= 1) {
difference = (diffInSec / day) + dayAgo;
} else {
difference = (diffInSec / day) + daysAgo;
}
} else if ((diffInSec / month) < 1) {
if ((diffInSec / week) <= 1) {
difference = (diffInSec / week) + weekAgo;
} else {
difference = (diffInSec / week) + weeksAgo;
}

} else if ((diffInSec / year) < 1) {
if ((diffInSec / month) <= 1) {
difference = (diffInSec / month) + monthAgo;
} else {
difference = (diffInSec / month) + monthsAgo;
}
} else {
// return date
Calendar c = Calendar.getInstance();
c.setTimeInMillis(fromDate);

SimpleDateFormat format_before = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");

difference = format_before.format(c.getTime());
}
Log.e("time difference is: ", "" + difference);
return difference;
}
}

and use this like this

TimeAgo.DateDifference(time);

Time ago for Android/java

What you want to display is called as the Relative time display. Android provides methods
to display time relative to your current time. You don't have to use any third party library just for this purpose.

You can use

DateUtils.getRelativeTimeSpanString(long time, long now, long minResolution)

Refer docs Here

eg.

DateUtils.getRelativeTimeSpanString(your_time_in_milliseconds, current_ time_in_millisecinds,DateUtils.MINUTE_IN_MILLIS);

UPDATE1:

You can try following to pass your date and get the milliseconds for it.

public static long getDateInMillis(String srcDate) {
SimpleDateFormat desiredFormat = new SimpleDateFormat(
"d MMMM yyyy, hh:mm aa");

long dateInMillis = 0;
try {
Date date = desiredFormat.parse(srcDate);
dateInMillis = date.getTime();
return dateInMillis;
} catch (ParseException e) {
Log.d("Exception while parsing date. " + e.getMessage());
e.printStackTrace();
}

return 0;
}

Hope it helps you.

Android - Converting Date to Time ago but it is returning 48 years ago

I will look at your code but I should definitely say that, 48 years ago reminds me 1 Jan 1970. Probably you're sending null or 0 as date to humanizer function.

Edit After Review:

I think the problem is that, you're calculating duration of a date, not difference. If you want to calculate a real date ago, you should send time difference to toDuration function.

Current behavior is quite normal because you send a x-x-2018 date to toDuration function and it returns 48 years ago basically(since zero is 1970). You should send the difference.
For example;

long difference = System.currentTimeMillis() - dateInMillisecond;
String humanizedDate = toDuration(difference);

How to get exact TimeAgo in Android

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

Android - convert String date into time ago date format

You can do this following with your existing the following:

String convTime = null;
try {

String ago_date = "2020-01-14 12:52:00", currant_date = "2020-01-18 12:52:45";

String suffix = "Ago";

SimpleDateFormat convert_date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(convert_date.parse(ago_date));
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(convert_date.parse(currant_date));
long millis1 = calendar1.getTimeInMillis();
long millis2 = calendar2.getTimeInMillis();
long diff = millis2 - millis1;
// Calculate difference in seconds
long diffSeconds = diff / 1000;
// Calculate difference in minutes
long diffMinutes = diff / (60 * 1000);
// Calculate difference in hours
long diffHours = diff / (60 * 60 * 1000);
// Calculate difference in days
long diffDays = diff / (24 * 60 * 60 * 1000);
// calculate how much month in days
long multipledays = diffDays;
int maonths = (int) multipledays / 30;
int reamainigdays = maonths % 30;
// Calculate the years of monthes
int getyears = maonths / 12;
int reamainigmothe = getyears % 12;
System.out.println("In calculated: ");

if (diffSeconds<60) {
convTime = diffSeconds+" Seconds "+suffix;
} else if (diffMinutes < 60) {
convTime = diffMinutes+" Minutes "+suffix;
} else if (diffHours < 24) {
convTime = diffHours+" Hours "+suffix;
} else if (diffDays >= 7) {
if (reamainigmothe!=0 && reamainigmothe <= getyears % 12) {
convTime = reamainigmothe + " Years " + suffix;
} else if (diffDays > 30) {
convTime = maonths + " Months " + suffix;
} else {
convTime = (diffDays / 7) + " Week " + suffix;
}
} else if (diffDays < 7) {
convTime = diffDays+" Days "+suffix;
}

System.out.println("In calculated: 222--"+convTime );

} catch (Exception e) {
e.printStackTrace();
}

Where ago_date will be the past date and currant_date needs to be replaced with the current date, which will work you.

String ago_date = "2020-01-14 12:52:00",  currant_date = "2020-01-18 12:52:45";

How to calculate time ago in Java?

Take a look at the PrettyTime library.

It's quite simple to use:

import org.ocpsoft.prettytime.PrettyTime;

PrettyTime p = new PrettyTime();
System.out.println(p.format(new Date()));
// prints "moments ago"

You can also pass in a locale for internationalized messages:

PrettyTime p = new PrettyTime(new Locale("fr"));
System.out.println(p.format(new Date()));
// prints "à l'instant"

As noted in the comments, Android has this functionality built into the android.text.format.DateUtils class.



Related Topics



Leave a reply



Submit