java判断时间 字符串时分秒是否合法 10:11:12

java判断时间 字符串时分秒是否合法 10:11:12,第1张

public static boolean checkTime(String time) {
        try {
            String[] split = time.split(":");
            if (Integer.parseInt(split[0]) > 23 || split[0].length() != 2) {
                return false;
            }
            if (Integer.parseInt(split[1]) > 59 || split[1].length() != 2) {
                return false;
            }
            if (Integer.parseInt(split[2]) > 59 || split[2].length() != 2) {
                return false;
            }
        } catch (Exception e) {
            //NumberFormatException or NullPointerException
            return false;
        }
        return true;
    }

很简单的代码 还是比较好用的 比正则表达式好些一哈哈

顺颂时祺

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/736184.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-28
下一篇 2022-04-28

发表评论

登录后才能评论

评论列表(0条)

保存