Java 日期字符串格式判断

/**
     * 判断日期格式
     * 2004-2-30 是无效的
     * 2020-04-31 是无效的
     * @param sDate
     * @return
     */
    public static boolean isLegalDate(String sDate , String format) {
        if(sDate == null || isEmpty(format)){
            return false;
        }
        DateFormat formatter = new SimpleDateFormat(format);
        try {
            Date date = formatter.parse(sDate);
            return sDate.equals(formatter.format(date));
        } catch (Exception e) {
            return false;
        }
    }

需求:根据输入的字符串和格式,判断是否符合需要的格式。

Guess you like

Origin blog.csdn.net/JavaAlpha/article/details/105790403