ExtDateTimeUtils { public static Date daysBeforeNow(int numDays) { if (numDays <= 0) { throw new IllegalArgumentException(); } final Calendar cal = new GregorianCalendar(); cal.setTime(new Date()); cal.add(Calendar.DAY_OF_YEAR, -numDays); return cal.getTime(); } private  ExtDateTimeUtils(); static String timestampToHumanDateAndTime(long timestamp); static String dateToHumanDateAndTime(Date date); static String dateToHuman(Date date); static Timestamp getCurrentTimeStamp(); static Date daysBefore(Date dateInst, int numDays); static Date daysBeforeNow(int numDays); static Date daysAfter(Date dateInst, int numDays); static Date daysAfterWithoutTimeZone(Date dateInst, int numDays); static Date daysAfterNow(int numDays); static Date startOfDay(Date dateInst); static Date endOfDay(Date dateInst); static Date startOfDayUsingOutputTimeZone(Date dateInst); static Date endOfDayUsingOutputTimeZone(Date dateInst); static final long MILLIS_IN_SECOND; static final long MILLIS_IN_MINUTE; static final long MILLIS_IN_HOUR; static final long MILLIS_IN_DAY; static final int LAST_HOUR_OF_DAY; static final int LAST_MINUTE; static final int LAST_SECOND; static final int LAST_MILLISECOND; }
@Test(expected = IllegalArgumentException.class) public void testDaysBeforeWithNegativeNumber(){ assertEquals("1 day before now","", ExtDateTimeUtils.daysBeforeNow(-1)); }
@Test public void testDaysBeforeNowWithRealDate(){ Date date = new Date(); date.getTime(); Date dateMinusOneDay = new Date(); long millisecondsInOneDay = 86400000L; dateMinusOneDay.setTime(date.getTime() - millisecondsInOneDay); Calendar cal = Calendar.getInstance(); cal.setTime(dateMinusOneDay); date = ExtDateTimeUtils.daysBeforeNow(1); Calendar cal2 = Calendar.getInstance(); cal2.setTime(date); assertEquals("1 day before input date ",cal.get(Calendar.DAY_OF_MONTH),cal2.get(Calendar.DAY_OF_MONTH)); }