Skip to content

Instantly share code, notes, and snippets.

@gsanthosh91
Created October 16, 2018 10:01
Show Gist options
  • Select an option

  • Save gsanthosh91/c4e97ed11f819e52a7a8a456984f90d5 to your computer and use it in GitHub Desktop.

Select an option

Save gsanthosh91/c4e97ed11f819e52a7a8a456984f90d5 to your computer and use it in GitHub Desktop.
WhastApp like displayable time
private String getDisplayableTime(long value) {
SimpleDateFormat formatter = new SimpleDateFormat("hh:mm a", Locale.getDefault());
Long mDate = System.currentTimeMillis();
if (mDate > value) {
long difference = mDate - value;
final long seconds = difference / 1000;
final long minutes = seconds / 60;
final long hours = minutes / 60;
final long days = hours / 24;
final long months = days / 31;
final long years = days / 365;
if (seconds < 86400) {
return formatter.format(new Date(value));
} else if (seconds < 172800) {// 48 * 60 * 60
return "yesterday";
} else if (seconds < 2592000) { // 30 * 24 * 60 * 60
return days + " days ago";
} else if (seconds < 31104000) { // 12 * 30 * 24 * 60 * 60
return months <= 1 ? "one month ago" : days + " months ago";
} else {
return years <= 1 ? "one year ago" : years + " years ago";
}
}
return "now";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment