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

 

方案一、使用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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值