DateUtils { public static String formatJava8Date(Class<?> clazz, Object value, String format) { if (LocalDate.class.equals(clazz)) { return formatDate((LocalDate) value, format); } else if (LocalDateTime.class.equals(clazz)) { return formatDateTime((LocalDateTime) value, format); } else if (LocalTime.class.equals(clazz)) { return formatTime((LocalTime) value, format); } else if (ZonedDateTime.class.equals(clazz)) { return formatZonedDateTime((ZonedDateTime) value, 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 testFormatJava8Date() { assertEquals("31-03-2014", DateUtils.formatJava8Date(LocalDate.class, DateUtils.createLocalDate("31032014"), "dd-MM-yyyy")); assertEquals("31-03-2014 05:06:07", DateUtils.formatJava8Date(LocalDateTime.class, DateUtils.createLocalDateTime("31032014 050607"), "dd-MM-yyyy HH:mm:ss")); assertEquals("05:06:07", DateUtils.formatJava8Date(LocalTime.class, DateUtils.createLocalTime("050607"), "HH:mm:ss")); }