java判断生日字符串是否合法

写了个判断用户输入生日字符串是否合法的方法,前提是输入字符串格式为yyyyMMdd。

public static boolean checkBirthDay(String birthday) {if (Common.empty(birthday)) {return false;}if (birthday.length() != 8) {return false;}Pattern pattern = Pattern.compile("^[1,2]\\d{3}(0[1-9]||1[0-2])(0[1-9]||[1,2][0-9]||3[0,1])$");Matcher matcher = pattern.matcher(birthday);if (!matcher.matches()) {return false;}Date birth = null;try {birth = new SimpleDateFormat("yyyyMMdd").parse(birthday);} catch (ParseException e) {e.printStackTrace();}if (!new SimpleDateFormat("yyyyMMdd").format(birth).equals(birthday)) {return false;}// 获取当前日期的毫秒数long currentTime = System.currentTimeMillis();// 获取生日的毫秒数long birthTime = birth.getTime();// 如果当前时间小于生日,,生日不合法。反之合法if (birthTime > currentTime) {return false;}return true;}

原文地址:

生气是拿别人做错的事来惩罚自己

java判断生日字符串是否合法

相关文章:

你感兴趣的文章:

标签云: