根据整数型生成日期格式
public String format(int num){
String time="";
String datestr = num+"";
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
try {
Date strToDate = sdf.parse(datestr);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(format.format(strToDate));
time=format.format(strToDate);
} catch (ParseException e) {
e.printStackTrace();
}
return time;
}
跟据当前日期获取年龄
public String age(String date_of_birth){
String age="";
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try {
Date parse = format.parse(date_of_birth);
Calendar cal = Calendar.getInstance();
cal.setTime(parse);
Calendar curr = Calendar.getInstance();
int ageYear=-(cal.get(Calendar.YEAR)-curr.get(Calendar.YEAR));
if (cal.get(Calendar.MONTH)>curr.get(Calendar.MONTH) || cal.get(Calendar.MONTH)==curr.get(Calendar.MONTH)&&cal.get(Calendar.DAY_OF_MONTH)>=curr.get(Calendar.DAY_OF_MONTH)){
ageYear--;
}
age=ageYear+"";
System.out.println("你今年:"+age+"岁了");
} catch (ParseException e) {
e.printStackTrace();
}
return age;
}