@Override protected String formatTime(DateTime now, DateTime then) { StringBuilder text = new StringBuilder(); if (hasFormat(HOURS)) { formatHours(now, then, text); } else if (hasFormat(MINUTES)) { formatMinutes(now, then, text); } else if (hasFormat(SECONDS)) { formatSeconds(now, then, text); } return text.toString(); }
@Override protected String formatDate(DateTime now, DateTime then) { StringBuffer text = new StringBuffer(); if (hasFormat(YEARS)) { formatYears(now, then, text); } else if (hasFormat(MONTHS)) { formatMonths(now, then, text); } else if (hasFormat(DAYS)) { formatDays(now, then, text); } if (hasFormat(TIME)) { StringBuilder pattern = new StringBuilder(); if ((format & HOURS) != 0) pattern.append("hh"); if ((format & MINUTES) != 0) pattern.append(pattern.length() == 0 ? "mm" : ":mm"); if ((format & SECONDS) != 0) pattern.append(pattern.length() == 0 ? "ss" : ":ss"); text.append(", " + DateTimeFormat.forPattern(pattern.toString()).print(then.toInstant())); } return text.toString(); }
private void formatMinutes(DateTime now, DateTime then, StringBuilder text) { int minutesBetween = Minutes.minutesBetween(now.toLocalTime(), then.toLocalTime()).getMinutes(); if (minutesBetween == 0) { if (hasFormat(SECONDS)) { formatSeconds(now, then, text); } else { text.append(context.getString(R.string.now)); } } else if (minutesBetween > 0) { // in N hours text.append(context.getResources().getQuantityString(R.plurals.carbon_inMinutes, minutesBetween, minutesBetween)); } else { // N hours ago text.append(context.getResources().getQuantityString(R.plurals.carbon_minutesAgo, -minutesBetween, -minutesBetween)); } }
private void formatHours(DateTime now, DateTime then, StringBuilder text) { int hoursBetween = Hours.hoursBetween(now.toLocalTime(), then.toLocalTime()).getHours(); if (hoursBetween == 0) { if (hasFormat(MINUTES)) { formatMinutes(now, then, text); } else { text.append(context.getString(R.string.now)); } } else if (hoursBetween > 0) { // in N hours text.append(context.getResources().getQuantityString(R.plurals.carbon_inHours, hoursBetween, hoursBetween)); } else { // N hours ago text.append(context.getResources().getQuantityString(R.plurals.carbon_hoursAgo, -hoursBetween, -hoursBetween)); } }