DateUtil { private DateUtil() { } private  DateUtil(); static Date convertToDate(String text, DateFormat format); static Date convertToDate(String text); static String now(); static String nowLong(); static String nowMid(); static String nowShort(); static String nowVerbose(); static String nowPlain(); static String convertToString(long millis, DateFormat format); static String convertToString(long millis); static String convertToStringLong(long millis); static String convertToStringMid(long millis); static String convertToStringShort(long millis); static String convertToStringVerbose(long millis); static String convertToStringPlain(long millis); static String convertToStringYearMonth(long millis);  }
@Test public void testDateUtil() throws Exception { String now = DateUtil.now(); String nowLong = DateUtil.nowLong(); String nowMid = DateUtil.nowMid(); String nowShort = DateUtil.nowShort(); assertTrue(nowLong.length() > now.length()); assertTrue(now.length() > nowMid.length()); assertTrue(nowMid.length() > nowShort.length()); long time = 1305662518734L; String dateRegex = "2011-05"; String dayRegex = "-1[7-8]"; String timeMinRegex = "[0-9][0-9]:[0,3]1"; String timeSec = ":58"; String timeSubSec = ".058"; String longDate = DateUtil.convertToStringLong(time); assertTrue(longDate, longDate.matches( dateRegex + dayRegex + "T" + timeMinRegex + timeSec + timeSubSec)); String defaultDate = DateUtil.convertToString(time); assertTrue(defaultDate, defaultDate.matches( dateRegex + dayRegex + "T" + timeMinRegex + timeSec)); String medDate = DateUtil.convertToStringMid(time); assertTrue(medDate, medDate.matches( dateRegex + dayRegex + "T" + timeMinRegex)); String shortDate = DateUtil.convertToStringShort(time); assertTrue(shortDate, shortDate.matches(dateRegex + dayRegex)); String yearMonthDate = DateUtil.convertToStringYearMonth(time); assertTrue(yearMonthDate, yearMonthDate.matches(dateRegex)); long roundedTime = 1305662518000L; Date dateToCheck = DateUtil.convertToDate(DateUtil.convertToString(roundedTime)); assertEquals(roundedTime, dateToCheck.getTime()); }