DateUtil { public static Date toUtcDate(String dateStr) { notNull(dateStr, "dateStr can't be null"); try { return buildDateFormat().parse(dateStr); } catch (ParseException e) { throw new RuntimeException(e); } }  static Date toUtcDate(String dateStr); static String toDateString(Instant instant); static String toUtcDateString(Instant instant); static Date now();  }
@Test public void testToUtcDate() { val format = new SimpleDateFormat(dateFormat); format.setTimeZone(TimeZone.getTimeZone("UTC")); Date actual = DateUtil.toUtcDate(instString); try { val expected = format.parse("2016-11-07 21:51:40"); assertEquals(actual, expected); } catch (ParseException e) { e.printStackTrace(); } }
@Test(expected = IllegalArgumentException.class) public void testToUtcDate_nullDateStr() { DateUtil.toUtcDate(null); }
@Test(expected = RuntimeException.class) public void testToUtcDate_invalidDateString() { DateUtil.toUtcDate("i_am_not_a_parseable_date"); }