import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Tests {
public static void main(String[] args) throws UnsupportedEncodingException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try {
Person person = new Person("Jack", 0, format.parse("1990-01-01"));
Calendar cal=Calendar.getInstance();
long l = cal.getTimeInMillis();
long a = l-person.getBirthday().getTime();
long b = a/1000/60/60/24;//换算为天
System.out.println(b/365+"岁");
} catch (ParseException e) {
e.printStackTrace();
}
}
}
class Person{
private String name;
private int sex;
private Date birthday;
public Person(String name, int sex, Date birthday) {
super();
this.name = name;
this.sex = sex;
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
}
出生日期用java.sql.Date
构造两个Calendar实例,一个是出生日期,一个是系统当前日期
用两个实例的get(Calendar.YEAR)减一下就出来了