Skip to content

Instantly share code, notes, and snippets.

@rozag
Last active July 23, 2018 10:14
Show Gist options
  • Save rozag/2583aaec0d4b4ab0e2af to your computer and use it in GitHub Desktop.
Save rozag/2583aaec0d4b4ab0e2af to your computer and use it in GitHub Desktop.
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 context) {
if (time < 1000000000000L) {
// if timestamp given in seconds, convert to millis
time *= 1000;
}
Calendar calendar = Calendar.getInstance();
long now = calendar.getTimeInMillis();
if (time > now || time <= 0) {
return null;
}
final long diff = now - time;
if (diff < MINUTE_MILLIS) {
return context.getResources().getString(R.string.just_now);
} else if (diff < 2 * MINUTE_MILLIS) {
return context.getResources().getString(R.string.minute_ago);
} else if (diff < 50 * MINUTE_MILLIS) {
return diff / MINUTE_MILLIS + " " + context.getResources().getString(R.string.minutes_ago);
} else if (diff < 90 * MINUTE_MILLIS) {
return context.getResources().getString(R.string.hour_ago);
} else if (diff < 24 * HOUR_MILLIS) {
return diff / HOUR_MILLIS + " " + context.getResources().getString(R.string.hours_ago);
} else if (diff < 48 * HOUR_MILLIS) {
return context.getResources().getString(R.string.yesterday);
} else {
return diff / DAY_MILLIS + " " + context.getResources().getString(R.string.days_ago);
}
}
@SENYANGE
Copy link

why is it that it always gives time ago as 00:00 doesn't change

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment