Java 日期格式化工具类(兼容 Date 与 LocalDateTime)

public class DateUtils {

    private static final String DEFAULT_DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static String format(Date date) {
        return format(date, DEFAULT_DATE_TIME_PATTERN);
    }

    public static String format(LocalDateTime date) {
        return format(date, DEFAULT_DATE_TIME_PATTERN);
    }

    public static String format(LocalDateTime date, String pattern) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return formatter.format(date);
    }

    public static String format(Date date, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }
}