Java determines whether a birthday string is legal

Source: Internet
Author: User

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

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.