Questions about survival date and age based on ID card in java

Source: Internet
Author: User

/* There is no big function or security verification, but the length of the entered ID card number is verified.
You can add other functions by yourself. */
Import java. util. *; import java. util. imports; import java. text. *; import java. util. date; class Test02 {public static void main (String [] args) {custom input = new partition (System. in); System. out. println ("Enter your ID number:"); String s = input. next (); while (s. length ()! = 18) // determine the number of inputs to preliminarily verify the authenticity of the ID card number {System. out. println ("your input is incorrect. Please enter it again! "); S = input. next () ;}// read the information using the substring method of String [] String yy1 = s. substring (6, 10); // The Year of birth String mm1 = s. substring (); // The month of birth String dd1 = s. substring (); // Date of Birth: System. out. println ("your birthday is:"); String birthday = yy1.concat ("-"). concat (mm1 ). concat ("-"). concat (dd1); // replace the 0 System in the string with replace. out. println (mm1.replace ("0", "") + "month" + dd1.replace ("0", "") + "); System. out. println ("Your Date of Birth:"); System. out. println (birthday); // calculate the age. try {Date date = new Date (); SimpleDateFormat sdf = new SimpleDateFormat ("yyy-MM-dd"); String s1 = sdf. format (date); Date today = sdf. parse (s1 );
/* The parse method can be used to query the api by yourself. It converts the document (here it is String) format to the sdf (custom Date Format ). */
Date birth = sdf. parse (birthday); System. out. println ("your age:"); System. out. println (today. getYear ()-birth. getYear (); // System. out. println (today); // System. out. println (today. getYear ();} catch (Exception e) {System. out. println ("a problem has happened! ");} System. out. println (" Thank you! ");}}
// Exception handling is performed here due to version issues

 

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.