1 ImportJava.text.SimpleDateFormat; 2 ImportJava.util.Calendar; 3 Importjava.util.Date; 4 5 /** 6 * Created by Qing on 2017/3/28.7 */ 8 Public classAgeutils {9 //birthtimestring: "1994-11-14" according to the age of the month and dayTen Public Static intgetagefrombirthtime (String birthtimestring) { One //first intercept the year, month, and day in the string AString strs[] = Birthtimestring.trim (). Split ("-"); - intSelectyear = Integer.parseint (strs[0]); - intSelectmonth = Integer.parseint (strs[1]); the intSelectday = Integer.parseint (strs[2]); - //get the current time of year, month, day -Calendar cal =calendar.getinstance (); - intYearnow =Cal.get (calendar.year); + intMonthnow = Cal.get (calendar.month) + 1; - intDaynow =Cal.get (calendar.date); + A //subtract birthday date from current month at intYearminus = Yearnow-selectyear; - intMonthminus = Monthnow-Selectmonth; - intDayminus = Daynow-Selectday; - - intage = Yearminus;//approximate Assignment First - if(Yearminus < 0) {//Choose the year of the future inAge = 0; -}Else if(Yearminus = = 0) {//the same year, either 1, or 0 . to if(Monthminus < 0) {//Choose the next month. +Age = 0; -}Else if(Monthminus = = 0) {//of the same month the if(Dayminus < 0) {//Choose a date for the future *Age = 0; $}Else if(Dayminus >= 0) { Panax NotoginsengAge = 1; - } the}Else if(Monthminus > 0) { +Age = 1; A } the}Else if(Yearminus > 0) { + if(Monthminus < 0) {//Current Month > Birthday month -}Else if(Monthminus = = 0) {//the same month, and then the age based on the date $ if(Dayminus < 0) { $}Else if(Dayminus >= 0) { -Age = Age + 1; - } the}Else if(Monthminus > 0) { -Age = Age + 1; Wuyi } the } - returnAge ; Wu } - About //calculate age based on timestamp $ Public Static intGetagefrombirthtime (LongBirthtimelong) { -Date Date =NewDate (Birthtimelong * 1000l); -SimpleDateFormat format =NewSimpleDateFormat ("Yyyy-mm-dd"); -String birthtimestring =Format.format (date); A returnGetagefrombirthtime (birthtimestring); + } the}
Accurate to Month:
1 //birthtimestring: "1994-11-14" according to the age of the month and day2 Public Staticstring Getagefrombirthtime (String birthtimestring) {3 //first intercept the year, month, and day in the string4String strs[] = Birthtimestring.trim (). Split ("-");5 intSelectyear = Integer.parseint (strs[0]);6 intSelectmonth = Integer.parseint (strs[1]);7 intSelectday = Integer.parseint (strs[2]);8 //get the current time of year, month, day9Calendar cal =calendar.getinstance ();Ten intYearnow =Cal.get (calendar.year); One intMonthnow = Cal.get (calendar.month) + 1; A intDaynow =Cal.get (calendar.date); - - //subtract birthday date from current month the intYearminus = Yearnow-selectyear; - intMonthminus = Monthnow-Selectmonth; - intDayminus = Daynow-Selectday; -String agetomonth= "January"; + - intage = Yearminus;//approximate Assignment First + if(Yearminus < 0) {//Choose the year of the future AAge = 0; atAgetomonth = "January"; -}Else if(Monthminus > 0) { -Age = 1; -Agetomonth = string.valueof (monthminus) + "Month"; -}Else if(Yearminus > 0) { - if(Monthminus < 0) {//Current Month > Birthday month in}Else if(Monthminus = = 0) {//the same month, and then the age based on the date - if(Dayminus < 0) { to}Else if(Dayminus >= 0) { +Age = Age + 1; -Agetomonth = string.valueof (age) + "years old"; the } *}Else if(Monthminus > 0) { $Age = Age + 1;Panax NotoginsengAgetomonth = string.valueof (age) + "years old"; - } the } + returnAgetomonth; A}
1 Public Static void Main (string[] args) {2 String Dataofbirth = "2017-10-28"; 3 String age = getagefrombirthtime (dataofbirth); 4 System.out.println ("Age:" + age ); 5 }
Java calculates age accurately based on date