DateUtils { public static Long parseDateAsTimestamp(String date) throws UnparsableDateFormatException { if (date != null) { try { return Long.valueOf(date); } catch (NumberFormatException e) { for (SimpleDateFormat f : formats) { try { Date d = f.parse(date); return d.getTime(); } catch (ParseException ex) { } } } } throw new UnparsableDateFormatException(); } private  DateUtils(); static Long parseDateAsTimestamp(String date);  }
@Test public void test() throws UnparsableDateFormatException { assertEquals(new Long(1483228800000L), DateUtils.parseDateAsTimestamp("2017-01-01")); assertEquals(new Long(1483228800000L), DateUtils.parseDateAsTimestamp("2017-01-01 00:00")); assertEquals(new Long(1483228800000L), DateUtils.parseDateAsTimestamp("01/01/2017")); assertEquals(new Long(1483228800000L), DateUtils.parseDateAsTimestamp("01/01/2017 00:00")); assertEquals(new Long(1483228800000L), DateUtils.parseDateAsTimestamp("1483228800000")); boolean exception = false; try { assertEquals(new Long(1483228800000L), DateUtils.parseDateAsTimestamp("mouaiccool")); } catch (UnparsableDateFormatException e) { exception = true; } assertTrue(exception); exception = false; try { assertEquals(new Long(1483228800000L), DateUtils.parseDateAsTimestamp("")); } catch (UnparsableDateFormatException e) { exception = true; } assertTrue(exception); exception = false; try { assertEquals(new Long(1483228800000L), DateUtils.parseDateAsTimestamp(null)); } catch (UnparsableDateFormatException e) { exception = true; } assertTrue(exception); }