DateTimeUtil { public static String formatDate(final Date date, final boolean withTime) { return formatDate(date, withTime ? PATTERN_CZ_DATETIME : PATTERN_CZ_DATE); } private  DateTimeUtil(); static String formatDuration(final long duration, final boolean longFormat, final boolean trimZeros); static Date now(); static Date nowZeroSec(); static Calendar nowCalendar(); static Date today(); static Calendar todayCalendar(); static Date yesterday(); static Calendar yesterdayCalendar(); static Date addTime(final Date date, final int period, final int amount); static Calendar addTimeCalendar(final Calendar calendar, final int period, final int amount); static Calendar date2calendar(final Date date); static Calendar parseDate(final String date, final boolean withTime); static Calendar parseDate(final String date, final String pattern); static Calendar parseISODateTime(final String dateTime); static Calendar parseDefaultISODateTime(final String dateTime); static String formatDate(final Date date, final boolean withTime); static String formatDate(final Date date, final String pattern); static String formatDateByLocale(final Date date, Locale locale, int style); static String formatDateTimeByLocale(final Date date, Locale locale, int style); static int extractYear(final Date date); static int getFirstDayOfWeek(Locale locale); static boolean inPast(Date time); static Date getMidnight(Date time); static final String PATTERN_CZ_DATE; static final String PATTERN_CZ_DATETIME; static final String PATTERN_CZ_TIME; static final String PATTERN_LOG; }
@Test public void testFormatDateDateBoolean() { assertNull(DateTimeUtil.formatDate(null, true)); assertNull(DateTimeUtil.formatDate(null, false)); assertEquals("30.12.2010 13:59:59", DateTimeUtil.formatDate(testDateTimeFull.toDate(), true)); assertEquals("30.12.2010 13:59:59", DateTimeUtil.formatDate(testDateTime.toDate(), true)); assertEquals("30.12.2010 00:00:00", DateTimeUtil.formatDate(testDate.toDate(), true)); assertEquals("30.12.2010", DateTimeUtil.formatDate(testDateTimeFull.toDate(), false)); assertEquals("30.12.2010", DateTimeUtil.formatDate(testDateTime.toDate(), false)); assertEquals("30.12.2010", DateTimeUtil.formatDate(testDate.toDate(), false)); }
@Test public void testFormatDateDateString() { assertNull(DateTimeUtil.formatDate(null, DateTimeUtil.PATTERN_CZ_DATE)); assertNull(DateTimeUtil.formatDate(new Date(), null)); assertNull(DateTimeUtil.formatDate(new Date(), "")); assertNull(DateTimeUtil.formatDate(new Date(), " \t \n ")); try { DateTimeUtil.formatDate(new Date(), "sasdfasdfg"); fail(); } catch (IllegalArgumentException expected) { } assertEquals("30.12.2010 13:59:59", DateTimeUtil.formatDate(testDateTimeFull.toDate(), DateTimeUtil.PATTERN_CZ_DATETIME)); assertEquals("30.12.2010 13:59:59", DateTimeUtil.formatDate(testDateTime.toDate(), DateTimeUtil.PATTERN_CZ_DATETIME)); assertEquals("30.12.2010 00:00:00", DateTimeUtil.formatDate(testDate.toDate(), DateTimeUtil.PATTERN_CZ_DATETIME)); assertEquals("30.12.2010 13:59:59.999", DateTimeUtil.formatDate(testDateTimeFull.toDate(), patternFull)); assertEquals("30.12.2010 13:59:59.000", DateTimeUtil.formatDate(testDateTime.toDate(), patternFull)); assertEquals("30.12.2010 00:00:00.000", DateTimeUtil.formatDate(testDate.toDate(), patternFull)); assertEquals("30.12.2010", DateTimeUtil.formatDate(testDateTimeFull.toDate(), DateTimeUtil.PATTERN_CZ_DATE)); assertEquals("30.12.2010", DateTimeUtil.formatDate(testDateTime.toDate(), DateTimeUtil.PATTERN_CZ_DATE)); assertEquals("30.12.2010", DateTimeUtil.formatDate(testDate.toDate(), DateTimeUtil.PATTERN_CZ_DATE)); }