@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 formatTime(DateTime now, DateTime then) { if (timeFormat == null) buildTimeFormat(); return timeFormat.print(then); }
public String format(long nowDateTime, long dateTime) { DateTime now = new DateTime(nowDateTime); DateTime then = new DateTime(dateTime); if ((format & DATE) != 0) return formatDate(now, then); return formatTime(now, then); }
}; NaturalDateFormat[] formats = new NaturalDateFormat[]{ new RelativeDateFormat(this, NaturalDateFormat.DATE), new RelativeDateFormat(this, NaturalDateFormat.TIME), new AbsoluteDateFormat(this, NaturalDateFormat.WEEKDAY | NaturalDateFormat.DATE), new AbsoluteDateFormat(this, NaturalDateFormat.DATE | NaturalDateFormat.HOURS | NaturalDateFormat.MINUTES) }; ((AbsoluteDateFormat)formats[2]).setAbbreviated(true); ((AbsoluteDateFormat)formats[3]).setTwelveHour(true); buffer.append(format.format(date)). append(" - "). append(formats[j].format(date.getTime())). append("\n");
@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(); }
@Override protected String formatDate(DateTime now, DateTime then) { if (dateFormat == null) buildDateFormat(); StringBuilder builder = new StringBuilder(); if (hasFormat(WEEKDAY) && now.get(omitWeekday) == then.get(omitWeekday)) builder.append(weekdayFormat.print(then)); if (hasFormat(DAYS | MONTHS)) { if (builder.length() > 0) builder.append(", "); builder.append(dateFormat.print(then)); } if (hasFormat(YEARS) && Years.yearsBetween(now, then).getYears() != 0) builder.append(yearFormat.print(then)); if (hasFormat(TIME) && now.get(omitTime) == then.get(omitTime)) { builder.append(", "); builder.append(formatTime(now, then)); } return builder.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)); } }
private void buildTimeFormat() { if (!hasFormat(TIME)) return; StringBuilder pattern = new StringBuilder(); if ((format & HOURS) != 0) pattern.append(twelveHour ? "h" : "H"); if ((format & MINUTES) != 0) pattern.append(pattern.length() == 0 ? "mm" : ":mm"); if ((format & SECONDS) != 0) pattern.append(pattern.length() == 0 ? "ss" : ":ss"); if (hasFormat(HOURS) && twelveHour) pattern.append(" a"); timeFormat = DateTimeFormat.forPattern(pattern.toString()); }
private void formatYears(DateTime now, DateTime then, StringBuffer text) { int yearsBetween = Years.yearsBetween(now.toLocalDate(), then.toLocalDate()).getYears(); if (yearsBetween == 0) { if ((format & MONTHS) != 0) { formatMonths(now, then, text); } else { text.append(context.getString(R.string.thisYear)); } } else if (yearsBetween > 0) { // in N years text.append(context.getResources().getQuantityString(R.plurals.carbon_inYears, yearsBetween, yearsBetween)); } else { // N years ago text.append(context.getResources().getQuantityString(R.plurals.carbon_yearsAgo, -yearsBetween, -yearsBetween)); } }
private void formatMonths(DateTime now, DateTime then, StringBuffer text) { int monthsBetween = Months.monthsBetween(now.toLocalDate(), then.toLocalDate()).getMonths(); if (monthsBetween == 0) { if ((format & DAYS) != 0) { formatDays(now, then, text); } else { text.append(context.getString(R.string.thisMonth)); } } else if (monthsBetween > 0) { // in N months text.append(context.getResources().getQuantityString(R.plurals.carbon_inMonths, monthsBetween, monthsBetween)); } else { // N months ago text.append(context.getResources().getQuantityString(R.plurals.carbon_monthsAgo, -monthsBetween, -monthsBetween)); } }
public String format(long dateTime) { DateTime now; if (chronology != null) { now = DateTime.now(chronology); } else { now = DateTime.now(DateTimeZone.forTimeZone(zone)); } DateTime then = new DateTime(dateTime); if ((format & DATE) != 0) return formatDate(now, then); return formatTime(now, then); }
private void buildDateFormat() { if (!hasFormat(DATE)) return; weekdayFormat = DateTimeFormat.forPattern(abbreviated ? "EEE" : "EEEEE"); if (zone != null) weekdayFormat.withZone(DateTimeZone.forTimeZone(zone)); if (chronology != null) weekdayFormat.withChronology(chronology); StringBuilder pattern = new StringBuilder(); if ((format & DAYS) != 0) pattern.append("d"); if ((format & MONTHS) != 0) { pattern.append(pattern.length() == 0 ? "" : " "); pattern.append(abbreviated ? "MMM" : "MMMMM"); } dateFormat = DateTimeFormat.forPattern(pattern.toString()); if (zone != null) dateFormat.withZone(DateTimeZone.forTimeZone(zone)); if (chronology != null) dateFormat.withChronology(chronology); yearFormat = DateTimeFormat.forPattern(" YYYY"); if (zone != null) yearFormat.withZone(DateTimeZone.forTimeZone(zone)); if (chronology != null) yearFormat.withChronology(chronology); }