DateUtils { public static Date fromString(String string, String format) { if (string == null) { throw new DateFormatException( "Cannot convert a null string into a date!"); } string = string.trim(); try { return new Date(FORMATTERS.get(format).parseMillis(string)); } catch (IllegalArgumentException error) { } if (format.contains("z")) { try { return new SimpleDateFormat(format).parse(string); } catch (ParseException error) { } } throw new DateFormatException(String.format( "Cannot convert [%s] into a date using the [%s] format!", string, format)); }  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 public void fromString_format_notz() throws ParseException { Date expect = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2011/05/13 12:45:27"); assertEquals(expect, DateUtils.fromString("2011/05/13 12:45:27", "yyyy/MM/dd HH:mm:ss")); }
@Test public void fromString_format_tz() throws ParseException { Date expect = new SimpleDateFormat("yyyy/MM/dd z HH:mm:ss").parse("2011/05/12 EDT 12:45:27"); assertEquals(expect, DateUtils.fromString("2011/05/12 EDT 12:45:27", "yyyy/MM/dd z HH:mm:ss")); }
@Test (expected=DateFormatException.class) public void fromString_format_wrongformat() { DateUtils.fromString("2011/05/12 EST 12:45:27", "yyyy-MM-dd z HH:mm:ss"); }
@Test (expected=IllegalArgumentException.class) public void fromString_format_invalidformat() { DateUtils.fromString("2011/05/12 EST 12:45:27", "yyyy/MM/dd z HH:mm:invalid"); }
@Test (expected=NullPointerException.class) public void fromString_format_noformat() { DateUtils.fromString("2011/05/12 EST 12:45:27", null); }
@Test (expected=DateFormatException.class) public void fromString_format_nodate() { DateUtils.fromString(null, "yyyy/MM/dd z HH:mm:invalid"); }
@Test public void fromString_noformat_knownformat() { DateUtils.fromString("2011-05-12 12:45:27"); }
@Test (expected=DateFormatException.class) public void fromString_noformat_unformat() { DateUtils.fromString("2011/05/12 12:45:27"); }
@Test (expected=DateFormatException.class) public void fromString_noformat_nodate() { DateUtils.fromString(null); }