TweetDateUtils { static String getRelativeTimeString(Resources res, long currentTimeMillis, long timestamp) { final long diff = currentTimeMillis - timestamp; if (diff >= 0) { if (diff < DateUtils.MINUTE_IN_MILLIS) { final int secs = (int) (diff / 1000); return res.getQuantityString(R.plurals.tw__time_secs, secs, secs); } else if (diff < DateUtils.HOUR_IN_MILLIS) { final int mins = (int) (diff / DateUtils.MINUTE_IN_MILLIS); return res.getQuantityString(R.plurals.tw__time_mins, mins, mins); } else if (diff < DateUtils.DAY_IN_MILLIS) { final int hours = (int) (diff / DateUtils.HOUR_IN_MILLIS); return res.getQuantityString(R.plurals.tw__time_hours, hours, hours); } else { final Calendar now = Calendar.getInstance(); now.setTimeInMillis(currentTimeMillis); final Calendar c = Calendar.getInstance(); c.setTimeInMillis(timestamp); final Date d = new Date(timestamp); if (now.get(Calendar.YEAR) == c.get(Calendar.YEAR)) { return RELATIVE_DATE_FORMAT.formatShortDateString(res, d); } else { return RELATIVE_DATE_FORMAT.formatLongDateString(res, d); } } } return RELATIVE_DATE_FORMAT.formatLongDateString(res, new Date(timestamp)); } private  TweetDateUtils();   }
@Test public void testGetRelativeTimeString_now() { assertEquals("0s", TweetDateUtils.getRelativeTimeString(resources, NOW_IN_MILLIS, NOW_IN_MILLIS)); }
@Test public void testGetRelativeTimeString_secondsAgo() { final long tenSecondsAgo = NOW_IN_MILLIS - DateUtils.SECOND_IN_MILLIS * 10; assertEquals("10s", TweetDateUtils.getRelativeTimeString(resources, NOW_IN_MILLIS, tenSecondsAgo)); }
@Test public void testGetRelativeTimeString_minutesAgo() { final long twoMinutesAgo = NOW_IN_MILLIS - DateUtils.MINUTE_IN_MILLIS * 2; assertEquals("2m", TweetDateUtils.getRelativeTimeString(resources, NOW_IN_MILLIS, twoMinutesAgo)); }
@Test public void testGetRelativeTimeString_hoursAgo() { final long twoHoursAgo = NOW_IN_MILLIS - DateUtils.HOUR_IN_MILLIS * 2; assertEquals("2h", TweetDateUtils.getRelativeTimeString(resources, NOW_IN_MILLIS, twoHoursAgo)); }
@Test public void testGetRelativeTimeString_daysAgo() { final long twoDaysAgo = NOW_IN_MILLIS - DateUtils.DAY_IN_MILLIS * 2; assertEquals("Mar 18", TweetDateUtils.getRelativeTimeString(resources, NOW_IN_MILLIS, twoDaysAgo)); }
@Test public void testGetRelativeTimeString_lessThanAYearAgoWithinSameYear() { final long sixtyDaysAgo = NOW_IN_MILLIS - DateUtils.DAY_IN_MILLIS * 60; assertEquals("Jan 19", TweetDateUtils.getRelativeTimeString(resources, NOW_IN_MILLIS, sixtyDaysAgo)); }
@Test public void testGetRelativeTimeString_moreThanAYearAgo() { final long twoYearsAgo = NOW_IN_MILLIS - DateUtils.DAY_IN_MILLIS * 730; assertEquals("03/20/12", TweetDateUtils.getRelativeTimeString(resources, NOW_IN_MILLIS, twoYearsAgo)); }
@Test public void testGetRelativeTimeString_inTheFuture() { final long twoYearsIntoTheFuture = NOW_IN_MILLIS + DateUtils.DAY_IN_MILLIS * 730; assertEquals("03/19/16", TweetDateUtils.getRelativeTimeString(resources, NOW_IN_MILLIS, twoYearsIntoTheFuture)); }
@Test public void testGetRelativeTimeString_negativeTime() { final long wayInthePast = -DateUtils.DAY_IN_MILLIS; assertEquals("12/31/69", TweetDateUtils.getRelativeTimeString(resources, NOW_IN_MILLIS, wayInthePast)); }
@Test public void testGetRelativeTimeString_zeroTime() { assertEquals("01/01/70", TweetDateUtils.getRelativeTimeString(resources, NOW_IN_MILLIS, 0)); }