1 import java.text.SimpleDateFormat; 2 import java.util.Calendar; 3 import java.util.Date; 4 5 /** 6 * Created by qing on 2017/3/28. 7 */ 8 public class AgeUtils { 9 // 根据年月日计算年龄,birthTimeString:"1994-11-14" 10 public static int getAgeFromBirthTime(String birthTimeString) { 11 // 先截取到字符串中的年、月、日 12 String strs[] = birthTimeString.trim().split("-"); 13 int selectYear = Integer.parseInt(strs[0]); 14 int selectMonth = Integer.parseInt(strs[1]); 15 int selectDay = Integer.parseInt(strs[2]); 16 // 得到当前时间的年、月、日 17 Calendar cal = Calendar.getInstance(); 18 int yearNow = cal.get(Calendar.YEAR); 19 int monthNow = cal.get(Calendar.MONTH) + 1; 20 int dayNow = cal.get(Calendar.DATE); 21 22 // 用当前年月日减去生日年月日 23 int yearMinus = yearNow - selectYear; 24 int monthMinus = monthNow - selectMonth; 25 int dayMinus = dayNow - selectDay; 26 27 int age = yearMinus;// 先大致赋值 28 if (yearMinus < 0) {// 选了未来的年份 29 age = 0; 30 } else if (yearMinus == 0) {// 同年的,要么为1,要么为0 31 if (monthMinus < 0) {// 选了未来的月份 32 age = 0; 33 } else if (monthMinus == 0) {// 同月份的 34 if (dayMinus < 0) {// 选了未来的日期 35 age = 0; 36 } else if (dayMinus >= 0) { 37 age = 1; 38 } 39 } else if (monthMinus > 0) { 40 age = 1; 41 } 42 } else if (yearMinus > 0) { 43 if (monthMinus < 0) {// 当前月>生日月 44 } else if (monthMinus == 0) {// 同月份的,再根据日期计算年龄 45 if (dayMinus < 0) { 46 } else if (dayMinus >= 0) { 47 age = age + 1; 48 } 49 } else if (monthMinus > 0) { 50 age = age + 1; 51 } 52 } 53 return age; 54 } 55 56 // 根据时间戳计算年龄 57 public static int getAgeFromBirthTime(long birthTimeLong) { 58 Date date = new Date(birthTimeLong * 1000l); 59 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 60 String birthTimeString = format.format(date); 61 return getAgeFromBirthTime(birthTimeString); 62 } 63 }
精确到月:
1 // 根据年月日计算年龄,birthTimeString:"1994-11-14" 2 public static String getAgeFromBirthTime(String birthTimeString) { 3 // 先截取到字符串中的年、月、日 4 String strs[] = birthTimeString.trim().split("-"); 5 int selectYear = Integer.parseInt(strs[0]); 6 int selectMonth = Integer.parseInt(strs[1]); 7 int selectDay = Integer.parseInt(strs[2]); 8 // 得到当前时间的年、月、日 9 Calendar cal = Calendar.getInstance(); 10 int yearNow = cal.get(Calendar.YEAR); 11 int monthNow = cal.get(Calendar.MONTH) + 1; 12 int dayNow = cal.get(Calendar.DATE); 13 14 // 用当前年月日减去生日年月日 15 int yearMinus = yearNow - selectYear; 16 int monthMinus = monthNow - selectMonth; 17 int dayMinus = dayNow - selectDay; 18 String ageToMonth= "1月"; 19 20 int age = yearMinus;// 先大致赋值 21 if (yearMinus < 0) {// 选了未来的年份 22 age = 0; 23 ageToMonth = "1月"; 24 } else if (monthMinus > 0) { 25 age = 1; 26 ageToMonth = String.valueOf(monthMinus)+"月"; 27 } else if (yearMinus > 0) { 28 if (monthMinus < 0) {// 当前月>生日月 29 } else if (monthMinus == 0) {// 同月份的,再根据日期计算年龄 30 if (dayMinus < 0) { 31 } else if (dayMinus >= 0) { 32 age = age + 1; 33 ageToMonth = String.valueOf(age)+"岁"; 34 } 35 } else if (monthMinus > 0) { 36 age = age + 1; 37 ageToMonth = String.valueOf(age)+"岁"; 38 } 39 } 40 return ageToMonth; 41 }
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 }