1 Public StaticString Getagetxt (String birthtime,string beginTime,intLevel ) {2 if(Stringutils.isblank (birthtime) | |Stringutils.isblank (BeginTime)) {3System.out.println ("There is a null value in the parameter! ");4 }5 6 intYear = 0,month=0,day=0,hour=0;7 8Date birthDate = getdatebystring (Birthtime, "Yyyy-mm-dd HH:mm:ss");9Date begindate = getdatebystring (BeginTime, "Yyyy-mm-dd HH:mm:ss");TenCalendar cbirthdate =calendar.getinstance (); OneCalendar cbegindate =calendar.getinstance (); A Cbirthdate.settime (birthDate); - Cbegindate.settime (begindate); - the if(Cbegindate.get (Calendar.year) <Cbirthdate.get (calendar.year)) { - return"Date of birth is greater than current time"; - } - + //Calculate Birth Hours - if(Cbegindate.get (Calendar.hour_of_day) <Cbirthdate.get (Calendar.hour_of_day)) { +hour = Cbegindate.get (calendar.hour_of_day) +24-Cbirthdate.get (calendar.hour_of_day); ADay = Day-1; at}Else{ -hour = Cbegindate.get (calendar.hour_of_day)-Cbirthdate.get (calendar.hour_of_day); - } - - //Calculate a birthday - if(Cbegindate.get (Calendar.day_of_month) <Cbirthdate.get (Calendar.day_of_month)) { inDay = day + cbegindate.get (calendar.day_of_month) + cbirthdate.getactualmaximum (calendar.day_of_month)-Cbirthdate.get (calendar.day_of_month); -month = Month-1; to}Else { +Day = day + cbegindate.get (calendar.day_of_month)-Cbirthdate.get (calendar.day_of_month); - } the * //Calculate Birth Month $ if(Cbegindate.get (Calendar.month) <Cbirthdate.get (Calendar.month)) {Panax NotoginsengMonth = month + cbegindate.get (calendar.month) +12-Cbirthdate.get (calendar.month); -Year = Year-1; the}Else{ +Month = month + cbegindate.get (calendar.month)-Cbirthdate.get (calendar.month); A } the + //calculate year of birth -Year = year + cbegindate.get (calendar.year)-Cbirthdate.get (calendar.year); $ $ if(Year >7){ - returnyear+ "Old"; -}Else if(Year >0 && year <7){ the if(month>0 && Month <10){ - returnyear+ "Year 0" +month+ "month";Wuyi}Else if(Month >=10 ){ the returnyear+ "Year" +month+ "month"; -}Else { Wu if(day>0){ - returnYear+ "Age 0" +day+ "Day"; About}Else{ $ returnyear+ "Old"; - } - } -}Else { A if(Month >0){ + if(Day >0 && Day <10){ the returnmonth+ "Month 0" +day+ "Day"; -}Else if(Day >=10){ $ returnmonth+ "months" +day+ "Days"; the}Else{ the returnmonth+ "Months"; the } the}Else{ - if(Day >0){ in if(Hour >0 && hour <10){ the returnday+ "Day 0" +hour+ "hour"; the}Else if(Hour >=10){ About returnday+ "Days" +hour+ "hours"; the}Else{ the returnDay+ "Day"; the } +}Else{ - if(hour>0){ the returnhour+ "Hours";Bayi}Else{ the return"Less than an hour of birth"; the } - } - } the } the}
One of the more important points
1. Calendar.getactualmaximum () calculates the maximum value in the corresponding range
2, the calculation age should be from small to large computing age, so convenient to calculate the age behind (using the principle of subtraction);
Java Computing Age