DateCache { public String formatFor(int cycle) { long millis = (long) cycle * cycleLength; int hash = Maths.hash(millis) & (SIZE - 1); DateValue dv = values[hash]; if (dv == null || dv.millis != millis) { synchronized (format) { String text = format.format(new Date(millis)); values[hash] = new DateValue(millis, text); return text; } } return dv.text; } DateCache(final String formatStr, int cycleLength); String formatFor(int cycle); long parseCount(String name);  }
@Test public void testFormat() { DateCache dc = new DateCache("yyyyMMdd", 86400000); String str = dc.formatFor(16067); assertEquals("20131228", str); String str1 = dc.formatFor(1); assertEquals("19700102", str1); }
@Test public void testFormatMillis() { String format = "yyyyMMddHHmmss"; SimpleDateFormat sdf = new SimpleDateFormat(format); sdf.setTimeZone(TimeZone.getTimeZone("GMT")); DateCache dc = new DateCache(format, 1000); int now = (int) (System.currentTimeMillis() / 1000); for (int i = 0; i < 10000; i++) { int now2 = now + i; String str2 = sdf.format(new Date(now2 * 1000L)); String str = dc.formatFor(now2); assertEquals("i: " + i, str2, str); } }