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