判断日期yyyymmdd

1 篇文章 0 订阅

 

转载 https://www.cnblogs.com/xiaostudy/p/12566327.html

 

   /**
     *  yyyyMMdd
     */
   String DATA_PATTERN_REG = "(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})(((0[13578]|1[02])(0[1-9]|[12][0-9]|3[01]))|"+
            "((0[469]|11)(0[1-9]|[12][0-9]|30))|(02(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|"+
            "((0[48]|[2468][048]|[3579][26])00))0229)$" ;



    /**
     * 判断是不是 yyyyMMdd 格式
     *
     * @param date
     * @return
     */
    public static boolean isYyyyMMdd(String date) {
        if (StringUtils.isBlank(date)) {
            return false;
        }
        Pattern pattern = Pattern.compile(DATA_PATTERN_REG);
        Matcher matcher = pattern.matcher(date);
        boolean matches = matcher.matches();
        if(matches){
            return true ;
        }else {
            return isRqFormat(date);
        }
    }




/**
 * 日期分隔符,处理特殊字符
 *
 * @param splitter
 * @return
 */
private static String dealWithSplitter(char splitter) {
    String strSplitter = String.valueOf(splitter);
    String[] str = {".", "+", "*", "|", "?"};
    if (Arrays.asList(str).contains(strSplitter)) {
        strSplitter = "[" + strSplitter + "]";
    } else if ("^".equals(strSplitter)) {
        strSplitter = "\\^";
    } else if ("(".equals(strSplitter)) {
        strSplitter = "\\(";
    } else if (")".equals(strSplitter)) {
        strSplitter = "\\)";
    } else if ("{".equals(strSplitter)) {
        strSplitter = "\\{";
    } else if ("}".equals(strSplitter)) {
        strSplitter = "\\}";
    } else if ("[".equals(strSplitter)) {
        strSplitter = "\\[";
    } else if ("]".equals(strSplitter)) {
        strSplitter = "\\]";
    }
    return strSplitter;
}

/***
 * 判断字符串是否是yyyyMMdd、yyyyMdd、yyyyMMd、yyyyMd格式,分隔符为splitter参数
 * 例如:yyyy-MM-dd、yyyy.MM.dd
 *
 * @param mes
 * @param splitter
 * @return
 */
public static boolean isRqFormat(String mes, char splitter){
    if (null == mes || 0 == mes.length()) {
        return false;
    }
    String strSplitter = dealWithSplitter(splitter);
    // 1000年——2020
//        String format = "(1[0-9]{3}|20([0-1]{1}[0-9]{1}|20))" + strSplitter + "([1-9]|0[1-9]|1[012])" + strSplitter + "([1-9]|0[1-9]|[12][0-9]|3[01])";
    // 0000年——9999年
    String format = "([0-9]{4})" + strSplitter + "([1-9]|0[1-9]|1[012])" + strSplitter + "([1-9]|0[1-9]|[12][0-9]|3[01])";
    Pattern pattern = Pattern.compile(format);
    Matcher matcher = pattern.matcher(mes);
    if (!matcher.matches()) {
        return false;
    }
    pattern = Pattern.compile("(\\d{4})" + strSplitter + "(\\d{1,2})" + strSplitter + "(\\d{1,2})");
    matcher = pattern.matcher(mes);
    if (!matcher.matches()) {
        return false;
    }
    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;
}

/***
 * 判断字符串是否是yyyyMMdd、yyyyMdd、yyyyMMd、yyyyMd格式
 *
 * @param mes
 * @return
 */
public static boolean isRqFormat(String mes){
    if (null == mes || 0 == mes.length()) {
        return false;
    }
    // 1000年——2020
//        String format = "(1[0-9]{3}|20([0-1]{1}[0-9]{1}|20))([1-9]|0[1-9]|1[012])([1-9]|0[1-9]|[12][0-9]|3[01])";
    // 0000年——9999年
    String format = "([0-9]{4})([1-9]|0[1-9]|1[012])([1-9]|0[1-9]|[12][0-9]|3[01])";
    Pattern pattern = Pattern.compile(format);
    Matcher matcher = pattern.matcher(mes);
    if (!matcher.matches()) {
        return false;
    }
    pattern = Pattern.compile("(\\d{4})(\\d{1,2})(\\d{1,2})");
    matcher = pattern.matcher(mes);
    if (!matcher.matches()) {
        return false;
    }
    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;
}

 


 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值