DateUtils { @SuppressWarnings("unchecked") public static <T> T createJava8Date(Class<T> clazz, String dateStr, String format) { if (LocalDate.class.equals(clazz)) { return (T) createLocalDate(dateStr, format); } else if (LocalDateTime.class.equals(clazz)) { return (T) createLocalDateTime(dateStr, format); } else if (LocalTime.class.equals(clazz)) { return (T) createLocalTime(dateStr, format); } else if (ZonedDateTime.class.equals(clazz)) { return (T) createZonedDateTime(dateStr, format); } return null; } private  DateUtils(); @SuppressWarnings("unchecked") static T createJava8Date(Class<T> clazz, String dateStr, String format); static LocalDate createLocalDate(String dateStr); static LocalDate createLocalDate(String dateStr, String format); static LocalDateTime createLocalDateTime(String dateTimeStr); static LocalDateTime createLocalDateTime(String dateTimeStr, String format); static LocalTime createLocalTime(String timeStr); static LocalTime createLocalTime(String timeStr, String format); static ZonedDateTime createZonedDateTime(String dateTimeStr); static ZonedDateTime createZonedDateTime(String dateTimeStr, String format); static String formatDate(LocalDate date, String format); static String formatDateTime(LocalDateTime dateTime, String format); static String formatJava8Date(Class<?> clazz, Object value, String format); static String formatTime(LocalTime time, String format); static String formatZonedDateTime(ZonedDateTime dateTime, String format); static int getLastWeekOfYear(int year); static String getNextWeekCode(String weekCode); static int getQuarter(LocalDate date); static LocalDate getStartDateOfWeek(String weekCode); static boolean isJava8DateType(Class<?> clazz); static boolean isSupportedDateType(Class<?> clazz); static boolean isValidWeekCode(String weekCode); static Date toLegacyDate(LocalDate d); static Date toLegacyDate(LocalDateTime ldt); static String toWeekCode(LocalDate date); static final String WEEK_CODE_PATTERN; }
@Test public void testCreateJava8Date() { assertNull(DateUtils.createJava8Date(LocalDate.class, null, "dd-MM-yyyy")); assertNull(DateUtils.createJava8Date(Date.class, "01-01-1980", "dd-MM-yyyy")); LocalDate d = DateUtils.createJava8Date(LocalDate.class, "01-01-1980", "dd-MM-yyyy"); assertEquals("1980-01-01", d.toString()); LocalTime lt = DateUtils.createJava8Date(LocalTime.class, "14:15:16", "HH:mm:ss"); assertEquals("14:15:16", lt.toString()); LocalDateTime ldt = DateUtils.createJava8Date(LocalDateTime.class, "01-02-1981 14:15:16", "dd-MM-yyyy HH:mm:ss"); assertEquals("1981-02-01T14:15:16", ldt.toString()); ZonedDateTime zdt = DateUtils.createJava8Date(ZonedDateTime.class, "01-02-1981 14:15:16+0100", "dd-MM-yyyy HH:mm:ssZ"); assertEquals("1981-02-01T14:15:16+01:00", zdt.toString()); }