DateUtil { public static Date string2Date(final String ds) { if (StringUtils.isEmpty(ds)) { return null; } String dateStr = ds.trim(); Date date = null; switch (dateStr.length()) { case DATE_LENGTH_8: date = string2Date(dateStr, DateConstants.DATE_FORMAT_8); break; case DATE_LENGTH_10: date = string2Date(dateStr, dateStr.indexOf("/") == -1 ? DateConstants.DATE_FORMAT_10 : DateConstants.DATE_FORMAT_10_2); break; case DATE_LENGTH_11: date = string2Date(dateStr, DateConstants.DATE_FORMAT_11); break; case DATE_LENGTH_14: date = string2Date(dateStr, DateConstants.DATETIME_FORMAT_14); break; case DATE_LENGTH_17: date = string2Date(dateStr, DateConstants.DATETIME_FORMAT_17); break; case DATE_LENGTH_19: date = string2Date(dateStr, dateStr.indexOf("/") == -1 ? DateConstants.DATETIME_FORMAT_19 : DateConstants.DATETIME_FORMAT_19_2); break; case DATE_LENGTH_21: date = string2Date(dateStr, DateConstants.DATETIME_FORMAT_21); break; case DATE_LENGTH_23: date = string2Date(dateStr, dateStr.indexOf("/") == -1 ? DateConstants.DATETIME_FORMAT_23 : DateConstants.DATETIME_FORMAT_23_2); break; default: throw new IllegalArgumentException(dateStr + "不支持的时间格式"); } return date; }  static Date string2Date(final String ds); static Date string2Date(final String date, final String format); static String date2String(final Date date); static String date2String(final Date date, final String format); static String getCurrentTimestamp(); static long getCurrentTime(); static Date getCurrentDate(); static int daysBetween(final Date startDate); static Date getYrMonthLastDay(final Date d); static Date getYrMonthFirstDay(final Date d); static int daysBetween(final Date startDate, final Date endDate);  }
@Test public void string2Date() { long t1 = NUM_A; long t2 = NUM_B; long t3 = NUM_C; String d8 = "20180912"; Date date = DateUtil.string2Date(d8); Assert.isTrue(date.getTime() == t1, ErrorCodeDef.SYSTEM_ERROR_10001); String d11 = "2018年09月12日"; date = DateUtil.string2Date(d11); Assert.isTrue(date.getTime() == t1, ErrorCodeDef.SYSTEM_ERROR_10001); String d10 = "2018-09-12"; date = DateUtil.string2Date(d10); Assert.isTrue(date.getTime() == t1, ErrorCodeDef.SYSTEM_ERROR_10001); String d102 = "2018/09/12"; date = DateUtil.string2Date(d102); Assert.isTrue(date.getTime() == t1, ErrorCodeDef.SYSTEM_ERROR_10001); String d14 = "20180912105355"; date = DateUtil.string2Date(d14); Assert.isTrue(date.getTime() == t2, ErrorCodeDef.SYSTEM_ERROR_10001); String d17 = "20180912105355123"; date = DateUtil.string2Date(d17); Assert.isTrue(date.getTime() == t3, ErrorCodeDef.SYSTEM_ERROR_10001); String d19 = "2018-09-12 10:53:55"; date = DateUtil.string2Date(d19); Assert.isTrue(date.getTime() == t2, ErrorCodeDef.SYSTEM_ERROR_10001); String d192 = "2018/09/12 10:53:55"; date = DateUtil.string2Date(d192); Assert.isTrue(date.getTime() == t2, ErrorCodeDef.SYSTEM_ERROR_10001); String d21 = "2018年09月12日 10时53分55秒"; date = DateUtil.string2Date(d21); Assert.isTrue(date.getTime() == t2, ErrorCodeDef.SYSTEM_ERROR_10001); String d23 = "2018-09-12 10:53:55.123"; date = DateUtil.string2Date(d23); Assert.isTrue(date.getTime() == t3, ErrorCodeDef.SYSTEM_ERROR_10001); String d232 = "2018/09/12 10:53:55.123"; date = DateUtil.string2Date(d232); Assert.isTrue(date.getTime() == t3, ErrorCodeDef.SYSTEM_ERROR_10001); String str = "18年9月12号10点53分55秒"; date = DateUtil.string2Date(str, "yy年M月dd号hh点mm分ss秒"); Assert.isTrue(date.getTime() == t2, ErrorCodeDef.SYSTEM_ERROR_10001); }