DateTools { public String howLongFromNow(final long then) { if (then != 0) { final long diff = System.currentTimeMillis() - then; final long totSec = diff / 1000; final int oneday = 86400; final int onehour = 3600; final int onemin = 60; final int days = Math.round(totSec / oneday); final int hours = Math.round(totSec - days * oneday) / onehour; final int minutes = Math.round(totSec - days * oneday - hours * onehour) / onemin; final int seconds = Math.round(totSec - days * oneday - hours * onehour - minutes * onemin); return days + " days, " + Tools.getDoubleDigit(hours) + ":" + Tools.getDoubleDigit(minutes) + ":" + Tools.getDoubleDigit(seconds); } else { return "0 days, 00:00:00"; } }  String currentDateToString(final String format); String dateToString(@Nullable final Date date, final String format); String howLongFromNow(final long then);  }
@Test public void howLongFromNowShouldReturnZeroIfThenIsZero() { final String howLongFromNow = dateTools.howLongFromNow(0); assertEquals("0 days, 00:00:00", howLongFromNow); }
@Test public void howLongFromNowShouldReturnCorrectWhenSeconds() { final DateTime then = new DateTime() .minusSeconds(3); final String howLongFromNow = dateTools.howLongFromNow(then.toDate().getTime()); assertEquals("0 days, 00:00:03", howLongFromNow); }
@Test public void howLongFromNowShouldReturnCorrectWhenMinutes() { final DateTime then = new DateTime() .minusSeconds(12) .minusMinutes(5); final String howLongFromNow = dateTools.howLongFromNow(then.toDate().getTime()); assertEquals("0 days, 00:05:12", howLongFromNow); }
@Test public void howLongFromNowShouldReturnCorrectWhenHours() { final DateTime then = new DateTime() .minusSeconds(51) .minusMinutes(22) .minusHours(5); final String howLongFromNow = dateTools.howLongFromNow(then.toDate().getTime()); assertEquals("0 days, 05:22:51", howLongFromNow); }
@Test public void howLongFromNowShouldReturnCorrectWhenDays() { final DateTime then = new DateTime() .minusSeconds(6) .minusMinutes(44) .minusHours(14) .minusDays(2); final String howLongFromNow = dateTools.howLongFromNow(then.toDate().getTime()); assertEquals("2 days, 14:44:06", howLongFromNow); }