Util { public static Date toDate(String dt) { SimpleDateFormat fmt = new SimpleDateFormat("MM/dd/yyyy"); try { return fmt.parse(dt); } catch (ParseException e) { throw new RuntimeException(e); } } private  Util(); static String generateUUID(); static String generateDbUUID(); static Set<T> asSet(T... values); static Date tomorrow(); static Date yesterday(); static Date midnight(); static Date addDaysToDt(int dayField); static Date addMinutesToDt(int minuteField); static Date toDate(String dt); static T assertNotNull(T value, String message); static boolean equals(String str, String str1); static void closeSafely(Object closable, String msg); static String capitalize(String str); static long generateUniqueLong(); static String toBase64(byte[] data); static SimpleDateFormat getUTCDateFormat(); static String readFile(InputStream is); static String hash(String password); static String toJson(Object anObject); static T fromJson(String json, Class<T> clazz); @SuppressWarnings("rawtypes") static String getClassName(Class c); static String getClassName(String fullClassName); static String reverseEndian(String in); static String transformUuid(String uuid); static List<String> getPossibleUuids(String... ids); static boolean isUuid(String uuid); static String collectionToString(Collection c); static boolean collectionsAreEqual(Collection<T> c1, Collection<T> c2); static boolean collectionsAreEqual(Collection<T> c1, Collection<T> c2, Comparator<T> comp); static String getHostname(); static final String UTC_STR; }
@Test public void toDate() { Calendar c = Calendar.getInstance(); c.setTime(Util.toDate("03/17/2011")); assertEquals(2, c.get(Calendar.MONTH)); assertEquals(17, c.get(Calendar.DAY_OF_MONTH)); assertEquals(2011, c.get(Calendar.YEAR)); }
@Test(expected = NullPointerException.class) public void toDateNull() { Util.toDate(null); }
@Test public void toDateShortForm() { Calendar c = Calendar.getInstance(); c.setTime(Util.toDate("3/1/11")); assertEquals(2, c.get(Calendar.MONTH)); assertEquals(1, c.get(Calendar.DAY_OF_MONTH)); assertEquals(11, c.get(Calendar.YEAR)); }
@Test(expected = RuntimeException.class) public void toDateMilitaryForm() { Util.toDate("17 March 2011"); }
@Test(expected = RuntimeException.class) public void toDateStdLongForm() { Util.toDate("March 17, 2011"); }