DateUtils { public static Date addTime(Date date, int field, int amount) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(field, amount); return calendar.getTime(); }  static Date fromString(String string, String format); static Date fromString(String string); static String toString(Date date, String format); static String toString(Date date); static String toLabel(Date date, boolean isShortened); static String toLabel(Date date); static boolean isSameDay(Date a, Date b); static Date addTime(Date date, int field, int amount); static String toSimpleElapsedTime(Date date);  }
@Test (expected=NullPointerException.class) public void addTime_null() throws ParseException { DateUtils.addTime(null, 0, 0); }
@Test public void toLabel_day() throws ParseException { Date start = new SimpleDateFormat("yyyy/MM/dd z HH:mm:ss").parse("2011/05/09 EST 05:00:00"); Date expect = new SimpleDateFormat("yyyy/MM/dd z HH:mm:ss").parse("2011/05/10 EST 05:00:00"); assertEquals(expect, DateUtils.addTime(start, Calendar.DAY_OF_MONTH, 1)); }
@Test public void toLabel_day_nextmonth() throws ParseException { Date start = new SimpleDateFormat("yyyy/MM/dd z HH:mm:ss").parse("2011/05/31 EST 05:00:00"); Date expect = new SimpleDateFormat("yyyy/MM/dd z HH:mm:ss").parse("2011/06/01 EST 05:00:00"); assertEquals(expect, DateUtils.addTime(start, Calendar.DAY_OF_MONTH, 1)); }
@Test public void toLabel_year() throws ParseException { Date start = new SimpleDateFormat("yyyy/MM/dd z HH:mm:ss").parse("2011/05/09 EST 05:00:00"); Date expect = new SimpleDateFormat("yyyy/MM/dd z HH:mm:ss").parse("2012/05/09 EST 05:00:00"); assertEquals(expect, DateUtils.addTime(start, Calendar.YEAR, 1)); }
@Test public void toLabel_month() throws ParseException { Date start = new SimpleDateFormat("yyyy/MM/dd z HH:mm:ss").parse("2011/05/10 EST 05:00:00"); Date expect = new SimpleDateFormat("yyyy/MM/dd z HH:mm:ss").parse("2011/06/10 EST 05:00:00"); assertEquals(expect, DateUtils.addTime(start, Calendar.MONTH, 1)); }
@Test public void toLabel_month_nextyear() throws ParseException { Date start = new SimpleDateFormat("yyyy/MM/dd z HH:mm:ss").parse("2011/12/09 EST 05:00:00"); Date expect = new SimpleDateFormat("yyyy/MM/dd z HH:mm:ss").parse("2012/01/09 EST 05:00:00"); assertEquals(expect, DateUtils.addTime(start, Calendar.MONTH, 1)); }
@Test public void toLabel_dow() throws ParseException { Date start = new SimpleDateFormat("E yyyy/MM/dd z HH:mm:ss").parse("Tue 2011/05/09 EST 24:00:00"); Date expect = new SimpleDateFormat("E yyyy/MM/dd z HH:mm:ss").parse("Wed 2011/05/10 EST 24:00:00"); System.out.println(new SimpleDateFormat("E yyyy/MM/dd z HH:mm:ss").format(expect)); assertEquals(expect, DateUtils.addTime(start, Calendar.DAY_OF_WEEK, 1)); }
@Test public void toLabel_dow_nextweek() throws ParseException { Date start = new SimpleDateFormat("E yyyy/MM/dd z HH:mm:ss").parse("Sat 2011/05/13 EST 24:00:00"); Date expect = new SimpleDateFormat("E yyyy/MM/dd z HH:mm:ss").parse("Mon 2011/05/15 EST 24:00:00"); assertEquals(expect, DateUtils.addTime(start, Calendar.DAY_OF_WEEK, 2)); }
@Test public void toLabel_negative() throws ParseException { Date start = new SimpleDateFormat("yyyy/MM/dd z HH:mm:ss").parse("2011/05/13 EST 24:00:00"); Date expect = new SimpleDateFormat("yyyy/MM/dd z HH:mm:ss").parse("2011/05/11 EST 24:00:00"); assertEquals(expect, DateUtils.addTime(start, Calendar.DAY_OF_MONTH, -2)); }