Skip to content

Instantly share code, notes, and snippets.

@HarshitKaushik
Created January 5, 2017 10:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HarshitKaushik/b4089f49d21030ebf525d49f7b828c1f to your computer and use it in GitHub Desktop.
Save HarshitKaushik/b4089f49d21030ebf525d49f7b828c1f to your computer and use it in GitHub Desktop.
public static String getActivityTime(Date inputDate) {
Date currentDate = new Date();
Long currentTime = new Date().getTime();
Long inputTime = inputDate.getTime();
long timeRemaining = currentTime - inputTime;
long sec = (timeRemaining / 1000) % 60;
long min = (timeRemaining / (1000 * 60)) % 60;
long hours = (timeRemaining / (1000 * 60 * 60)) % 24;
long days = (timeRemaining / (1000 * 60 * 60 * 24));
/* long weeks = (timeRemaining/(1000*60*60*24*7)); */
long months = (timeRemaining / ((long) 1000 * 60 * 60 * 24 * 30));
long year = (timeRemaining / ((long) 1000 * 60 * 60 * 24 * 365));
String activityTime = null;
if (sec < 60) {
activityTime = "moments ago";
}
if (min > 0) {
if (hours == 1) {
activityTime = min + " min ago";
} else {
activityTime = min + " mins ago";
}
}
if (hours > 0) {
if (hours == 1) {
activityTime = hours + " hour ago";
} else {
activityTime = hours + " hours ago";
}
}
if (days > 0) {
if (days == 1) {
activityTime = days + " day ago";
} else {
activityTime = days + " days ago";
}
}
if (months > 0) {
if (months == 1) {
activityTime = months + " month ago";
} else {
activityTime = months + " months ago";
}
}
if (year > 0) {
if (year == 1) {
activityTime = year + " year ago";
} else {
activityTime = year + " years ago";
}
}
if (activityTime == null) {
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yy HH:mm:ss");
activityTime = dateFormat.format(currentDate);
}
return activityTime;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment