Java Computing Age

Source: Internet
Author: User

Accurate to day calculation age

  Public Static intGetagebycardid (String card)throwsException {Integer len=card.length (); if(len = =china_id_min_length) {Card=conver15cardto18 (card); } String birthDate= Card.substring (6, 14); SimpleDateFormat SDF=NewSimpleDateFormat ("YyyyMMdd"); Date Date=Sdf.parse (birthDate); returncalcage (date); } /*** From date of birth to age accurate to day *@paramBirthDay *@return     * @throwsException*/     Public Static intCalcAge (Date BirthDay)throwsException {Calendar cal=calendar.getinstance (); if(Cal.before (BirthDay)) {Throw NewIllegalArgumentException ("Date of birth cannot be later than current date"); }          intYearnow =Cal.get (calendar.year); intMonthnow =Cal.get (Calendar.month); intDayofmonthnow =Cal.get (Calendar.day_of_month);            Cal.settime (BirthDay); intYearbirth =Cal.get (calendar.year); intMonthbirth =Cal.get (Calendar.month); intDayofmonthbirth =Cal.get (Calendar.day_of_month); intAge = Yearnow-Yearbirth; if(Monthnow <=Monthbirth) {              if(Monthnow = =Monthbirth) {                  if(Dayofmonthnow < Dayofmonthbirth) age--; }Else{ Age--; }          }          returnAge ; }

Age on an annual basis

 /*** Get age by ID number * *@paramIdcard * Identification number *@returnAge*/     Public Static intGetagebyidcard (String idcard) {intiage = 0; if(idcard.length () = =china_id_min_length) {Idcard=conver15cardto18 (Idcard); } String Year= Idcard.substring (6, 10); Calendar Cal=calendar.getinstance (); intIcurryear =Cal.get (calendar.year); Iage= Icurryear-integer.valueof (year); returniage; }

Java Computing Age

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.