Created
June 19, 2017 05:30
-
-
Save swarawan/a278295afae0d1ef2929a94a09a284a2 to your computer and use it in GitHub Desktop.
Pretty date format to get duration.
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 class DateUtils { | |
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