Java determines whether the birthday string is valid

Source: Internet
Author: User

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 ;}


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.