DateTimeUtil { public static Date addTime(final Date date, final int period, final int amount) { return addTimeCalendar(date2calendar(date), period, amount).getTime(); } 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 testAddTime() { assertEquals("30.12.2010 13:59:59.999", DateTimeUtil.formatDate(testDateTimeFull.toDate(), patternFull)); assertTrue(Date.class.equals(DateTimeUtil.addTime(testDateTimeFull.toDate(), Calendar.MILLISECOND, 0).getClass())); assertEquals(DateTimeUtil.formatDate(new Date(), false), DateTimeUtil.formatDate(DateTimeUtil.addTime(null, Calendar.YEAR, 0), false)); assertEquals("30.12.2010 13:59:59.999", DateTimeUtil.formatDate(DateTimeUtil.addTime(testDateTimeFull.toDate(), Calendar.MILLISECOND, 0), patternFull)); assertEquals("30.12.2010 14:00:00.000", DateTimeUtil.formatDate(DateTimeUtil.addTime(testDateTimeFull.toDate(), Calendar.MILLISECOND, 1), patternFull)); assertEquals("30.12.2010 13:59:59.998", DateTimeUtil.formatDate(DateTimeUtil.addTime(testDateTimeFull.toDate(), Calendar.MILLISECOND, -1), patternFull)); assertEquals("30.12.2010 13:59:59.999", DateTimeUtil.formatDate(DateTimeUtil.addTime(testDateTimeFull.toDate(), Calendar.SECOND, 0), patternFull)); assertEquals("30.12.2010 14:00:00.999", DateTimeUtil.formatDate(DateTimeUtil.addTime(testDateTimeFull.toDate(), Calendar.SECOND, 1), patternFull)); assertEquals("30.12.2010 13:59:58.999", DateTimeUtil.formatDate(DateTimeUtil.addTime(testDateTimeFull.toDate(), Calendar.SECOND, -1), patternFull)); assertEquals("30.12.2010 13:59:59.999", DateTimeUtil.formatDate(DateTimeUtil.addTime(testDateTimeFull.toDate(), Calendar.MINUTE, 0), patternFull)); assertEquals("30.12.2010 14:00:59.999", DateTimeUtil.formatDate(DateTimeUtil.addTime(testDateTimeFull.toDate(), Calendar.MINUTE, 1), patternFull)); assertEquals("30.12.2010 13:58:59.999", DateTimeUtil.formatDate(DateTimeUtil.addTime(testDateTimeFull.toDate(), Calendar.MINUTE, -1), patternFull)); assertEquals("30.12.2010 13:59:59.999", DateTimeUtil.formatDate(DateTimeUtil.addTime(testDateTimeFull.toDate(), Calendar.HOUR_OF_DAY, 0), patternFull)); assertEquals("30.12.2010 14:59:59.999", DateTimeUtil.formatDate(DateTimeUtil.addTime(testDateTimeFull.toDate(), Calendar.HOUR_OF_DAY, 1), patternFull)); assertEquals("30.12.2010 12:59:59.999", DateTimeUtil.formatDate(DateTimeUtil.addTime(testDateTimeFull.toDate(), Calendar.HOUR_OF_DAY, -1), patternFull)); assertEquals("30.12.2010 13:59:59.999", DateTimeUtil.formatDate(DateTimeUtil.addTime(testDateTimeFull.toDate(), Calendar.HOUR, 0), patternFull)); assertEquals("30.12.2010 14:59:59.999", DateTimeUtil.formatDate(DateTimeUtil.addTime(testDateTimeFull.toDate(), Calendar.HOUR, 1), patternFull)); assertEquals("30.12.2010 12:59:59.999", DateTimeUtil.formatDate(DateTimeUtil.addTime(testDateTimeFull.toDate(), Calendar.HOUR, -1), patternFull)); assertEquals("30.12.2010 13:59:59.999", DateTimeUtil.formatDate(DateTimeUtil.addTime(testDateTimeFull.toDate(), Calendar.HOUR, 0), patternFull)); assertEquals("30.12.2010 14:59:59.999", DateTimeUtil.formatDate(DateTimeUtil.addTime(testDateTimeFull.toDate(), Calendar.HOUR, 1), patternFull)); assertEquals("30.12.2010 12:59:59.999", DateTimeUtil.formatDate(DateTimeUtil.addTime(testDateTimeFull.toDate(), Calendar.HOUR, -1), patternFull)); assertEquals("30.12.2010 13:59:59.999", DateTimeUtil.formatDate(DateTimeUtil.addTime(testDateTimeFull.toDate(), Calendar.DAY_OF_MONTH, 0), patternFull)); assertEquals("01.01.2011 13:59:59.999", DateTimeUtil.formatDate(DateTimeUtil.addTime(testDateTimeFull.toDate(), Calendar.DAY_OF_MONTH, 2), patternFull)); assertEquals("29.12.2010 13:59:59.999", DateTimeUtil.formatDate(DateTimeUtil.addTime(testDateTimeFull.toDate(), Calendar.DAY_OF_MONTH, -1), patternFull)); assertEquals("30.12.2010 13:59:59.999", DateTimeUtil.formatDate(DateTimeUtil.addTime(testDateTimeFull.toDate(), Calendar.DATE, 0), patternFull)); assertEquals("01.01.2011 13:59:59.999", DateTimeUtil.formatDate(DateTimeUtil.addTime(testDateTimeFull.toDate(), Calendar.DATE, 2), patternFull)); assertEquals("29.12.2010 13:59:59.999", DateTimeUtil.formatDate(DateTimeUtil.addTime(testDateTimeFull.toDate(), Calendar.DATE, -1), patternFull)); assertEquals("19.01.2011 13:59:59.999", DateTimeUtil.formatDate(DateTimeUtil.addTime(testDateTimeFull.toDate(), Calendar.DATE, 20), patternFull)); assertEquals("30.12.2010 13:59:59.999", DateTimeUtil.formatDate(DateTimeUtil.addTime(testDateTimeFull.toDate(), Calendar.MONTH, 0), patternFull)); assertEquals("28.02.2011 13:59:59.999", DateTimeUtil.formatDate(DateTimeUtil.addTime(testDateTimeFull.toDate(), Calendar.MONTH, 2), patternFull)); assertEquals("30.11.2010 13:59:59.999", DateTimeUtil.formatDate(DateTimeUtil.addTime(testDateTimeFull.toDate(), Calendar.MONTH, -1), patternFull)); assertEquals("30.12.2010 13:59:59.999", DateTimeUtil.formatDate(DateTimeUtil.addTime(testDateTimeFull.toDate(), Calendar.YEAR, 0), patternFull)); assertEquals("30.12.2011 13:59:59.999", DateTimeUtil.formatDate(DateTimeUtil.addTime(testDateTimeFull.toDate(), Calendar.YEAR, 1), patternFull)); assertEquals("30.12.2009 13:59:59.999", DateTimeUtil.formatDate(DateTimeUtil.addTime(testDateTimeFull.toDate(), Calendar.YEAR, -1), patternFull)); }