Wrote a method to determine whether a user entered a birthday string, provided the input string format is YYYYMMDD.
public static Boolean checkbirthday (String birthday) {if (Common.empty (birthday)) {return false; } if (Birthday.length ()! = 8) {return false; The pattern pattern = pattern. Compile ("^[1,2]\\d{3} (0[1-9]| | 1[0-2]) (0[1-9]| | [Up] [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; }//Gets the number of milliseconds for the current date long currenttime = System.currenttimemillis (); Gets the number of milliseconds for a birthday long birthtime = Birth.gettime (); If the current time is less than the birthday, the birthday is not valid. Conversely legal if (Birthtime > CurrentTime) {return false; } return true; }
Original address: http://www.iyuze.cn/article/609.html
Java determines whether a birthday string is legal