@Override protected String formatTime(DateTime now, DateTime then) { if (timeFormat == null) buildTimeFormat(); return timeFormat.print(then); }
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()); }
@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(); } }
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);
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); }