package com.ruoyi.cc.utils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

public final class DateValidatorUtils {

    private static final DateTimeFormatter YMD_HMS =
            DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    private static final DateTimeFormatter ISO_LOCAL_DATE_TIME =
            DateTimeFormatter.ISO_LOCAL_DATE_TIME;

    /**
     * 校验字符串是否符合 yyyy-MM-dd HH:mm:ss
     */
    public static boolean isYmdHms(String str) {
        return isValid(str, YMD_HMS);
    }

    /**
     * 通用校验：给定任意 formatter
     */
    public static boolean isValid(String str, DateTimeFormatter fmt) {
        if (str == null || str.trim().isEmpty()) return false;
        try {
            fmt.parse(str.trim());   // 也可以直接 fmt.parse(str, LocalDateTime::from)
            return true;
        } catch (DateTimeParseException e) {
            return false;
        }
    }

    /* ===== 测试 ===== */
    public static void main(String[] args) {
        System.out.println(isYmdHms("2024-08-26 15:20:00")); // true
        System.out.println(isYmdHms("2024-08-26 25:70:00")); // false
    }
}
