Created
October 16, 2018 10:01
-
-
Save gsanthosh91/c4e97ed11f819e52a7a8a456984f90d5 to your computer and use it in GitHub Desktop.
WhastApp like displayable time
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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