判断字符串是否为日期格式

方案一、使用SimpleDateFormat判断

如果sdf.parse(dateStr)出现异常,说明一定是不符合日期(yyyyMMddHHmmss)格式的字符串,

但是不出现异常不一定是符合日期的字符串,比如20191301010101,我们知道,一年中没有第13月,但是SimpleDateFormat会将其解析为2020年第一个月。

所以用SimpledateDateFormat判断也不完全可行。

方案二、使用正则表达式判断

1、判断字符串是否是yyyyMMddHHmmss格式的日期

/***
     * 判断字符串是否是yyyyMMddHHmmss格式
     * @param mes 字符串
     * @return boolean 是否是日期格式
     */
    public static boolean isRqSjFormat(String mes){
        String format = "([0-9]{4})(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])"
                + "([01][0-9]|2[0-3])[0-5][0-9][0-5][0-9]";
        Pattern pattern = Pattern.compile(format);
        Matcher matcher = pattern.matcher(mes);
        if (matcher.matches()) {
            pattern = Pattern.compile("(\d{4})(\d{2})(\d{2}).*");
            matcher = pattern.matcher(mes);
            if (matcher.matches()) {
                int y = Integer.valueOf(matcher.group(1));
                int m = Integer.valueOf(matcher.group(2));
                int d = Integer.valueOf(matcher.group(3));
                if (d > 28) {
                    Calendar c = Calendar.getInstance();
                    c.set(y, m-1, 1);
            //每个月的最大天数
int lastDay = c.getActualMaximum(Calendar.DAY_OF_MONTH); return (lastDay >= d); } } return true; } return false; }

2、判断字符串是否是yyyyMMdd格式的日期

/***
     * 判断字符串是否是yyyyMMdd格式
     * @param mes 字符串
     * @return boolean 是否是日期格式
     */
    public static boolean isRqFormat(String mes){
        String format = "([0-9]{4})(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])";
        Pattern pattern = Pattern.compile(format);
        Matcher matcher = pattern.matcher(mes);
        if (matcher.matches()) {
            pattern = Pattern.compile("(\d{4})(\d{2})(\d{2})");
            matcher = pattern.matcher(mes);
            if (matcher.matches()) {
                int y = Integer.valueOf(matcher.group(1));
                int m = Integer.valueOf(matcher.group(2));
                int d = Integer.valueOf(matcher.group(3));
                if (d > 28) {
                    Calendar c = Calendar.getInstance();
                    c.set(y, m-1, 1);
            //每个月的最大天数
int lastDay = c.getActualMaximum(Calendar.DAY_OF_MONTH); return (lastDay >= d); } } return true; } return false; }

其他方案

1、通过计算和比较来判断字符串是否是HHmmss格式

/***
     * 判断字符串是否是HHmmss格式
     * @param mes 字符串
     * @return boolean 是否是日期格式
     */
    public static boolean isSjFormat(String mes){
        if(mes.length()!=6){
            return false;
        }
        String regex="^\d+$";
        if(!mes.matches(regex)){
            return false;
        }
        int h;
        int m;
        int s;
        try {
            h=Integer.parseInt(mes.substring(0, 2));
            m=Integer.parseInt(mes.substring(2,4));
            s=Integer.parseInt(mes.substring(4, 6));
            if(h>23||h<0||m>59||m<0||s>59||s<0){
                return false;
            }
        } catch (Exception e) {
            LoggerUtil.info(mes+"不是HHmmss时间格式的字符串");
            return false;
        }
        return true;
    }

参考地址 http://blog.jdk5.com/zh/java-validate-date-time-string/

原文地址:https://www.cnblogs.com/kiko2014551511/p/11547922.html