PluginDateUtils { public static void formatDate(Locale locale, String fmt, long instant, String tzName, StringBuilder buf) { DateTimeZone zone = null; try { zone = DateTimeZone.forID(tzName); } catch (IllegalArgumentException e) { zone = DateTimeZone.getDefault(); } DateTime date = new DateTime(instant, zone); int index = 0; int len = fmt.length(); while (index < len) { char c1 = fmt.charAt(index); index++; if (c1 != '%' || index == len) { buf.append(c1); continue; } char c2 = fmt.charAt(index); switch (c2) { case 'A': buf.append(date.dayOfWeek().getAsText(locale)); break; case 'a': buf.append(date.dayOfWeek().getAsShortText(locale)); break; case 'B': buf.append(date.monthOfYear().getAsText(locale)); break; case 'b': buf.append(date.monthOfYear().getAsShortText(locale)); break; case 'C': leftPad(date.centuryOfEra().get(), '0', 2, buf); break; case 'c': formatAggregate(DateTimeAggregate.FULL, locale, date, buf); break; case 'D': formatAggregate(DateTimeAggregate.MMDDYY, locale, date, buf); break; case 'd': leftPad(date.dayOfMonth().get(), '0', 2, buf); break; case 'e': leftPad(date.dayOfMonth().get(), ' ', 2, buf); break; case 'F': formatAggregate(DateTimeAggregate.YYYYMMDD, locale, date, buf); break; case 'G': buf.append(date.year().get()); break; case 'g': leftPad(date.yearOfCentury().get(), '0', 2, buf); break; case 'H': leftPad(date.hourOfDay().get(), '0', 2, buf); break; case 'h': buf.append(date.monthOfYear().getAsShortText(locale)); break; case 'I': leftPad(date.get(DateTimeFieldType.clockhourOfHalfday()), '0', 2, buf); break; case 'j': leftPad(date.dayOfYear().get(), '0', 3, buf); break; case 'k': leftPad(date.get(DateTimeFieldType.clockhourOfDay()), ' ', 2, buf); break; case 'l': leftPad(date.get(DateTimeFieldType.clockhourOfHalfday()), ' ', 2, buf); break; case 'M': leftPad(date.minuteOfHour().get(), '0', 2, buf); break; case 'm': leftPad(date.monthOfYear().get(), '0', 2, buf); break; case 'n': buf.append('\n'); break; case 'P': buf.append(date.get(DateTimeFieldType.halfdayOfDay()) == 0 ? "am" : "pm"); break; case 'p': buf.append(date.get(DateTimeFieldType.halfdayOfDay()) == 0 ? "AM" : "PM"); break; case 'R': formatAggregate(DateTimeAggregate.H240_M0, locale, date, buf); break; case 'S': leftPad(date.secondOfMinute().get(), '0', 2, buf); break; case 's': buf.append(instant / 1000); break; case 't': buf.append('\t'); break; case 'T': formatAggregate(DateTimeAggregate.H240_M0, locale, date, buf); buf.append(':'); leftPad(date.secondOfMinute().get(), '0', 2, buf); break; case 'U': leftPad(date.weekOfWeekyear().get(), '0', 2, buf); break; case 'u': buf.append(date.dayOfWeek().get()); break; case 'V': leftPad(date.weekOfWeekyear().get(), '0', 2, buf); break; case 'v': leftPad(date.dayOfMonth().get(), ' ', 2, buf); buf.append('-'); buf.append(date.monthOfYear().getAsShortText()); buf.append('-'); buf.append(date.getYear()); break; case 'W': break; case 'w': buf.append(date.dayOfWeek().get()); break; case 'X': formatAggregate(DateTimeAggregate.HHMMSSP, locale, date, buf); break; case 'x': formatAggregate(DateTimeAggregate.MMDDYYYY, locale, date, buf); break; case 'Y': buf.append(date.getYear()); break; case 'y': leftPad(date.getYearOfCentury(), '0', 2, buf); break; case 'Z': buf.append(zone.getNameKey(date.getMillis())); break; case 'z': int offset = date.getZone().getOffset(instant) / 60000; int hours = (int)Math.floor(offset / 60); int minutes = (hours * 60) - offset; if (offset < 0) { buf.append('-'); } leftPad(Math.abs(hours), '0', 2, buf); leftPad(Math.abs(minutes), '0', 2, buf); break; default: buf.append(c1).append(c2); } index++; } } private  PluginDateUtils(); static void humanizeDate(long instantMs, long now, boolean showSeconds, StringBuilder buf); static void humanizeDate(long instantMs, long baseMs, String tzId, boolean showSeconds, StringBuilder buf); static boolean sameDay(long instant1, long instant2, String tzName); static void formatDate(Locale locale, String fmt, long instant, String tzName, StringBuilder buf); static String getTimeZoneNameFromContext(Context ctx);  }
@Test public void testBasics() throws CodeException { String format = "%Y-%m-%d %H:%M:%S %Z"; assertEquals(formatDate(format, MAY_13_2013_010000_UTC, TZ_UTC), "2013-05-13 01:00:00 UTC"); assertEquals(formatDate(format, MAY_13_2013_010000_UTC, TZ_NY), "2013-05-12 21:00:00 EDT"); assertEquals(formatDate(format, MAY_13_2013_010000_UTC, TZ_LA), "2013-05-12 18:00:00 PDT"); assertEquals(formatDate(format, NOV_15_2013_123030_UTC, TZ_NY), "2013-11-15 07:30:30 EST"); assertEquals(formatDate(format, NOV_15_2013_123030_UTC, TZ_LA), "2013-11-15 04:30:30 PST"); }
@Test public void testSingleFields() throws CodeException { assertEquals(formatDate("%A", JAN_01_1970_071510_UTC, TZ_UTC), "Thursday"); assertEquals(formatDate("%A", MAY_13_2013_010000_UTC, TZ_UTC), "Monday"); assertEquals(formatDate("%A", NOV_15_2013_123030_UTC, TZ_UTC), "Friday"); assertEquals(formatDate("%A", JAN_01_1970_071510_UTC, TZ_UTC, MEXICO), "jueves"); assertEquals(formatDate("%A", MAY_13_2013_010000_UTC, TZ_UTC, MEXICO), "lunes"); assertEquals(formatDate("%A", NOV_15_2013_123030_UTC, TZ_UTC, MEXICO), "viernes"); assertEquals(formatDate("%a", JAN_01_1970_071510_UTC, TZ_UTC), "Thu"); assertEquals(formatDate("%a", MAY_13_2013_010000_UTC, TZ_UTC), "Mon"); assertEquals(formatDate("%a", NOV_15_2013_123030_UTC, TZ_UTC), "Fri"); assertEquals(formatDate("%B", JAN_01_1970_071510_UTC, TZ_UTC), "January"); assertEquals(formatDate("%B", MAY_13_2013_010000_UTC, TZ_UTC), "May"); assertEquals(formatDate("%B", NOV_15_2013_123030_UTC, TZ_UTC), "November"); assertEquals(formatDate("%B", JAN_01_1970_071510_UTC, TZ_UTC, MEXICO), "enero"); assertEquals(formatDate("%B", MAY_13_2013_010000_UTC, TZ_UTC, MEXICO), "mayo"); assertEquals(formatDate("%B", NOV_15_2013_123030_UTC, TZ_UTC, MEXICO), "noviembre"); assertEquals(formatDate("%b", JAN_01_1970_071510_UTC, TZ_UTC), "Jan"); assertEquals(formatDate("%b", MAY_13_2013_010000_UTC, TZ_UTC), "May"); assertEquals(formatDate("%b", NOV_15_2013_123030_UTC, TZ_UTC), "Nov"); assertEquals(formatDate("%C", JAN_01_1970_071510_UTC, TZ_UTC), "19"); assertEquals(formatDate("%C", MAY_13_2013_010000_UTC, TZ_UTC), "20"); assertEquals(formatDate("%d", JAN_01_1970_071510_UTC, TZ_UTC), "01"); assertEquals(formatDate("%d", MAY_13_2013_010000_UTC, TZ_UTC), "13"); assertEquals(formatDate("%e", JAN_01_1970_071510_UTC, TZ_UTC), " 1"); assertEquals(formatDate("%d", MAY_13_2013_010000_UTC, TZ_UTC), "13"); assertEquals(formatDate("%G", JAN_01_1970_071510_UTC, TZ_UTC), "1970"); assertEquals(formatDate("%G", MAY_13_2013_010000_UTC, TZ_UTC), "2013"); assertEquals(formatDate("%g", JAN_01_1970_071510_UTC, TZ_UTC), "70"); assertEquals(formatDate("%g", MAY_13_2013_010000_UTC, TZ_UTC), "13"); assertEquals(formatDate("%H", JAN_01_1970_071510_UTC, TZ_NY), "02"); assertEquals(formatDate("%H", MAY_13_2013_010000_UTC, TZ_NY), "21"); assertEquals(formatDate("%H", NOV_15_2013_123030_UTC, TZ_NY), "07"); assertEquals(formatDate("%h", MAY_13_2013_010000_UTC, TZ_UTC, Locale.GERMANY), "Mai"); assertEquals(formatDate("%h", MAY_13_2013_010000_UTC, TZ_UTC, MEXICO), "may"); assertEquals(formatDate("%I", JAN_01_1970_071510_UTC, TZ_NY), "02"); assertEquals(formatDate("%I", MAY_13_2013_010000_UTC, TZ_NY), "09"); assertEquals(formatDate("%I", NOV_15_2013_123030_UTC, TZ_NY), "07"); assertEquals(formatDate("%j", JAN_01_1970_071510_UTC, TZ_NY), "001"); assertEquals(formatDate("%j", MAY_13_2013_010000_UTC, TZ_NY), "132"); assertEquals(formatDate("%j", NOV_15_2013_123030_UTC, TZ_NY), "319"); assertEquals(formatDate("%k", JAN_01_1970_071510_UTC, TZ_NY), " 2"); assertEquals(formatDate("%k", MAY_13_2013_010000_UTC, TZ_NY), "21"); assertEquals(formatDate("%k", NOV_15_2013_123030_UTC, TZ_NY), " 7"); assertEquals(formatDate("%l", JAN_01_1970_071510_UTC, TZ_NY), " 2"); assertEquals(formatDate("%l", MAY_13_2013_010000_UTC, TZ_NY), " 9"); assertEquals(formatDate("%l", NOV_15_2013_123030_UTC, TZ_NY), " 7"); assertEquals(formatDate("%M", JAN_01_1970_071510_UTC, TZ_NY), "15"); assertEquals(formatDate("%M", MAY_13_2013_010000_UTC, TZ_NY), "00"); assertEquals(formatDate("%M", NOV_15_2013_123030_UTC, TZ_NY), "30"); assertEquals(formatDate("%m", JAN_01_1970_071510_UTC, TZ_NY), "01"); assertEquals(formatDate("%m", MAY_13_2013_010000_UTC, TZ_NY), "05"); assertEquals(formatDate("%m", NOV_15_2013_123030_UTC, TZ_NY), "11"); assertEquals(formatDate("%n", JAN_01_1970_071510_UTC, TZ_UTC), "\n"); assertEquals(formatDate("%P", JAN_01_1970_071510_UTC, TZ_UTC), "am"); assertEquals(formatDate("%P", MAY_13_2013_010000_UTC, TZ_UTC), "am"); assertEquals(formatDate("%P", NOV_15_2013_123030_UTC, TZ_UTC), "pm"); assertEquals(formatDate("%P", JAN_01_1970_071510_UTC, TZ_NY), "am"); assertEquals(formatDate("%P", MAY_13_2013_010000_UTC, TZ_NY), "pm"); assertEquals(formatDate("%P", NOV_15_2013_123030_UTC, TZ_NY), "am"); assertEquals(formatDate("%p", JAN_01_1970_071510_UTC, TZ_UTC), "AM"); assertEquals(formatDate("%p", MAY_13_2013_010000_UTC, TZ_UTC), "AM"); assertEquals(formatDate("%p", NOV_15_2013_123030_UTC, TZ_UTC), "PM"); assertEquals(formatDate("%p", JAN_01_1970_071510_UTC, TZ_NY), "AM"); assertEquals(formatDate("%p", MAY_13_2013_010000_UTC, TZ_NY), "PM"); assertEquals(formatDate("%p", NOV_15_2013_123030_UTC, TZ_NY), "AM"); assertEquals(formatDate("%S", JAN_01_1970_071510_UTC, TZ_UTC), "10"); assertEquals(formatDate("%S", MAY_13_2013_010000_UTC, TZ_UTC), "00"); assertEquals(formatDate("%S", NOV_15_2013_123030_UTC, TZ_UTC), "30"); assertEquals(formatDate("%s", JAN_01_1970_071510_UTC, TZ_UTC), "26110"); assertEquals(formatDate("%s", MAY_13_2013_010000_UTC, TZ_UTC), "1368406800"); assertEquals(formatDate("%s", NOV_15_2013_123030_UTC, TZ_UTC), "1384518630"); assertEquals(formatDate("%t", JAN_01_1970_071510_UTC, TZ_UTC), "\t"); assertEquals(formatDate("%Y", JAN_01_1970_071510_UTC, TZ_UTC), "1970"); assertEquals(formatDate("%Y", MAY_13_2013_010000_UTC, TZ_UTC), "2013"); assertEquals(formatDate("%Y", NOV_15_2013_123030_UTC, TZ_UTC), "2013"); assertEquals(formatDate("%y", JAN_01_1970_071510_UTC, TZ_UTC), "70"); assertEquals(formatDate("%y", MAY_13_2013_010000_UTC, TZ_UTC), "13"); assertEquals(formatDate("%y", NOV_15_2013_123030_UTC, TZ_UTC), "13"); assertEquals(formatDate("%Z", JAN_01_1970_071510_UTC, TZ_UTC), "UTC"); assertEquals(formatDate("%Z", MAY_13_2013_010000_UTC, TZ_NY), "EDT"); assertEquals(formatDate("%Z", NOV_15_2013_123030_UTC, TZ_LA), "PST"); assertEquals(formatDate("%z", JAN_01_1970_071510_UTC, TZ_UTC), "0000"); assertEquals(formatDate("%z", MAY_13_2013_010000_UTC, TZ_NY), "-0400"); assertEquals(formatDate("%z", NOV_15_2013_123030_UTC, TZ_LA), "-0800"); assertEquals(formatDate("%%", JAN_01_1970_071510_UTC, TZ_UTC), "%%"); assertEquals(formatDate("%,", MAY_13_2013_010000_UTC, TZ_NY), "%,"); }
@Test public void testAggregateFields() throws CodeException { String r1 = formatDate("%c", NOV_15_2013_123030_UTC, TZ_NY); String r2 = formatDate("%a %d %b %Y %I:%M:%S %p %Z", NOV_15_2013_123030_UTC, TZ_NY); assertEquals(r1, r2); r1 = formatDate("%c", MAY_13_2013_010000_UTC, TZ_NY); r2 = formatDate("%a %d %b %Y %I:%M:%S %p %Z", MAY_13_2013_010000_UTC, TZ_NY); assertEquals(r1, r2); r1 = formatDate("%F", NOV_15_2013_123030_UTC, TZ_LA); r2 = formatDate("%Y-%m-%d", NOV_15_2013_123030_UTC, TZ_LA); assertEquals(r1, r2); r1 = formatDate("%D", NOV_15_2013_123030_UTC, TZ_NY); r2 = formatDate("%m/%d/%y", NOV_15_2013_123030_UTC, TZ_NY); assertEquals(r1, r2); r1 = formatDate("%x", NOV_15_2013_123030_UTC, TZ_NY); r2 = formatDate("%m/%d/%Y", NOV_15_2013_123030_UTC, TZ_NY); assertEquals(r1, r2); r1 = formatDate("%R", NOV_15_2013_123030_UTC, TZ_NY); r2 = formatDate("%H:%M", NOV_15_2013_123030_UTC, TZ_NY); assertEquals(r1, r2); r1 = formatDate("%T", MAY_13_2013_010000_UTC, TZ_NY); r2 = formatDate("%H:%M:%S", MAY_13_2013_010000_UTC, TZ_NY); assertEquals(r1, r2); r1 = formatDate("%X", NOV_15_2013_123030_UTC, TZ_NY); r2 = formatDate("%I:%M:%S %p", NOV_15_2013_123030_UTC, TZ_NY); assertEquals(r1, r2); r1 = formatDate("%X", MAY_13_2013_010000_UTC, TZ_NY); r2 = formatDate("%I:%M:%S %p", MAY_13_2013_010000_UTC, TZ_NY); assertEquals(r1, r2); r1 = formatDate("%v", MAY_13_2013_010000_UTC, TZ_NY); r2 = formatDate("%e-%b-%Y", MAY_13_2013_010000_UTC, TZ_NY); assertEquals(r1, r2); }
@Test public void testAmPm() { String format = "%p %P %l"; assertEquals(formatDate(format, MAY_13_2013_010000_UTC, TZ_NY), "PM pm 9"); assertEquals(formatDate(format, NOV_15_2013_123030_UTC, TZ_NY), "AM am 7"); }
@Test public void testTimezoneOffsets() { String format = "%Z %z"; assertEquals(formatDate(format, MAY_13_2013_010000_UTC, TZ_NY), "EDT -0400"); assertEquals(formatDate(format, MAY_13_2013_010000_UTC, TZ_LA), "PDT -0700"); assertEquals(formatDate(format, NOV_15_2013_123030_UTC, TZ_NY), "EST -0500"); assertEquals(formatDate(format, NOV_15_2013_123030_UTC, TZ_LA), "PST -0800"); }