Skip to content

Instantly share code, notes, and snippets.

@BenoitDuffez
Last active August 29, 2015 13:58
Show Gist options
  • Save BenoitDuffez/9996012 to your computer and use it in GitHub Desktop.
Save BenoitDuffez/9996012 to your computer and use it in GitHub Desktop.
public static String getDeltaDateText(Context context, Calendar date) {
final String formattedDate;
if (date != null && date.getTimeInMillis() > 10000) {
long delta = (new GregorianCalendar().getTimeInMillis() - date.getTimeInMillis()) / 1000;
if (delta < 60) {
formattedDate = context.getString(R.string.time_delay_moments);
} else if (delta < 3600) {
formattedDate = MessageFormat.format(context.getString(R.string.time_delay_minutes), (int) (delta / 60));
} else if (delta < 3600 * 24) {
formattedDate = MessageFormat.format(context.getString(R.string.time_delay_hours), (int) (delta / 3600));
} else if (delta < 3600 * 24 * 30) {
formattedDate = MessageFormat.format(context.getString(R.string.time_delay_days), (int) (delta / (3600 * 24)));
} else if (delta < 3600 * 24 * 365) {
formattedDate = MessageFormat.format(context.getString(R.string.time_delay_months), (int) (delta / (3600 * 24 * 30)));
} else {
formattedDate = MessageFormat.format(context.getString(R.string.time_delay_years), (int) (delta / (3600 * 24 * 365)));
}
} else {
formattedDate = "";
}
return formattedDate;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment