Skip to content

Instantly share code, notes, and snippets.

@swarawan
Created September 14, 2017 01:12
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 swarawan/c168a34fec0197d8559c089847fa1f61 to your computer and use it in GitHub Desktop.
Save swarawan/c168a34fec0197d8559c089847fa1f61 to your computer and use it in GitHub Desktop.
Get duration depend on inserted Date.
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