DateFormatUtil { public static String formatFriendlyTimeSpanByNow(@NotNull Date date) { return formatFriendlyTimeSpanByNow(date.getTime()); }  static Date parseDate(@NotNull String pattern, @NotNull String dateString); static String formatDate(@NotNull String pattern, @NotNull Date date); static String formatDate(@NotNull String pattern, long date); static String formatDuration(@NotNull Date startDate, @NotNull Date endDate); static String formatDuration(long durationMillis); static String formatDurationOnSecond(@NotNull Date startDate, @NotNull Date endDate); static String formatDurationOnSecond(long durationMillis); static String formatFriendlyTimeSpanByNow(@NotNull Date date); static String formatFriendlyTimeSpanByNow(long timeStampMillis); static final String PATTERN_ISO; static final String PATTERN_ISO_ON_SECOND; static final String PATTERN_ISO_ON_DATE; static final String PATTERN_DEFAULT; static final String PATTERN_DEFAULT_ON_SECOND; static final FastDateFormat ISO_FORMAT; static final FastDateFormat ISO_ON_SECOND_FORMAT; static final FastDateFormat ISO_ON_DATE_FORMAT; static final FastDateFormat DEFAULT_FORMAT; static final FastDateFormat DEFAULT_ON_SECOND_FORMAT; }
@Test public void formatFriendlyTimeSpanByNow() throws ParseException { try { Date now = DateFormatUtil.DEFAULT_ON_SECOND_FORMAT.parse("2016-12-11 23:30:00"); ClockUtil.useDummyClock(now); Date lessOneSecond = DateFormatUtil.DEFAULT_FORMAT.parse("2016-12-11 23:29:59.500"); assertThat(DateFormatUtil.formatFriendlyTimeSpanByNow(lessOneSecond)).isEqualTo("刚刚"); Date lessOneMinute = DateFormatUtil.DEFAULT_FORMAT.parse("2016-12-11 23:29:55.000"); assertThat(DateFormatUtil.formatFriendlyTimeSpanByNow(lessOneMinute)).isEqualTo("5秒前"); Date lessOneHour = DateFormatUtil.DEFAULT_ON_SECOND_FORMAT.parse("2016-12-11 23:00:00"); assertThat(DateFormatUtil.formatFriendlyTimeSpanByNow(lessOneHour)).isEqualTo("30分钟前"); Date today = DateFormatUtil.DEFAULT_ON_SECOND_FORMAT.parse("2016-12-11 1:00:00"); assertThat(DateFormatUtil.formatFriendlyTimeSpanByNow(today)).isEqualTo("今天01:00"); Date yesterday = DateFormatUtil.DEFAULT_ON_SECOND_FORMAT.parse("2016-12-10 1:00:00"); assertThat(DateFormatUtil.formatFriendlyTimeSpanByNow(yesterday)).isEqualTo("昨天01:00"); Date threeDayBefore = DateFormatUtil.DEFAULT_ON_SECOND_FORMAT.parse("2016-12-09 1:00:00"); assertThat(DateFormatUtil.formatFriendlyTimeSpanByNow(threeDayBefore)).isEqualTo("2016-12-09"); } finally { ClockUtil.useDefaultClock(); } }