Skip to content

Instantly share code, notes, and snippets.

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 riyazMuhammad/5dc8271fb4986adb8878a9cdac93ada7 to your computer and use it in GitHub Desktop.
Save riyazMuhammad/5dc8271fb4986adb8878a9cdac93ada7 to your computer and use it in GitHub Desktop.
days spent
/**
* @param fromDate
* @return
*/
public static String getDateDiffPastInDays(Long fromDate) {
Date currentDate = new Date();
if (currentDate.getTime() <= fromDate) {
return "";
} else {
long days = TimeUnit.MILLISECONDS.toDays(new Date().getTime() - fromDate);
if (days > 365) {
return days / 365 + (days / 365 > 1 ? " years ago" : " year ago");
} else if (days > 30) {
return days / 30 + (days / 30 > 1 ? " months ago" : " month ago");
} else {
if (days == 0) {
return "Today";
} else {
return days + (days > 1 ? " days ago" : " day ago");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment