Java determines whether the birthday string is valid
You have written a method to determine whether the birthday string is valid, provided that the input string is in the format of 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;} // obtain the number of milliseconds of the current date long currentTime = System. currentTimeMillis (); // get the number of milliseconds for the birthday long birthTime = birth. getTime (); // if the current time is less than the birthday, the birthday is invalid. Otherwise, valid if (birthTime> currentTime) {return false;} return true ;}