Created
September 14, 2017 01:12
-
-
Save swarawan/c168a34fec0197d8559c089847fa1f61 to your computer and use it in GitHub Desktop.
Get duration depend on inserted Date.
This file contains 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
public static String getDurationDate(Date date) { | |
Date today = new Date(); | |
long diff = today.getTime() - date.getTime(); | |
long diffSeconds = diff / 1000 % 60; | |
long diffMinutes = diff / (60 * 1000) % 60; | |
long diffHours = diff / (60 * 60 * 1000) % 24; | |
long diffDays = diff / (24 * 60 * 60 * 1000) % 30; | |
long diffWeeks = diff / (30 * 24 * 60 * 60 * 1000) / 4; | |
long diffMonth = diff / (30 * 24 * 60 * 60 * 1000); | |
if (diffMonth > 0) { | |
return diffMonth + " bulan yang lalu"; | |
} else if (diffWeeks > 0) { | |
return diffWeeks + " minggu yang lalu"; | |
} else if (diffDays > 0) { | |
return diffDays + " hari yang lalu"; | |
} else if (diffHours > 0) { | |
return diffHours + " jam yang lalu"; | |
} else if (diffMinutes > 0) { | |
return diffMinutes + " menit yang lalu"; | |
} else { | |
return "Baru saja"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment