package test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class test01 {
@SuppressWarnings("deprecation")
public static void main(String[] args) throws Exception {
// TODO 自動生成されたメソッド・スタブ
Date dbDate = null;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dbDate = (Date) dateFormat.parse("1990-05-08");
System.out.println(getAge(dbDate));
}
public static int getAge(Date dateOfBirth) {
int age = 0;
Calendar born = Calendar.getInstance();
Calendar now = Calendar.getInstance();
if (dateOfBirth != null) {
now.setTime(new Date());
born.setTime(dateOfBirth);
if (born.after(now)) {
throw new IllegalArgumentException(
"Can't be born in the future");
}
age = now.get(Calendar.YEAR) - born.get(Calendar.YEAR);
if (now.get(Calendar.DAY_OF_YEAR) < born.get(Calendar.DAY_OF_YEAR)) {
age -= 1;
}
}
return age;
}
}
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class test01 {
@SuppressWarnings("deprecation")
public static void main(String[] args) throws Exception {
// TODO 自動生成されたメソッド・スタブ
Date dbDate = null;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dbDate = (Date) dateFormat.parse("1990-05-08");
System.out.println(getAge(dbDate));
}
public static int getAge(Date dateOfBirth) {
int age = 0;
Calendar born = Calendar.getInstance();
Calendar now = Calendar.getInstance();
if (dateOfBirth != null) {
now.setTime(new Date());
born.setTime(dateOfBirth);
if (born.after(now)) {
throw new IllegalArgumentException(
"Can't be born in the future");
}
age = now.get(Calendar.YEAR) - born.get(Calendar.YEAR);
if (now.get(Calendar.DAY_OF_YEAR) < born.get(Calendar.DAY_OF_YEAR)) {
age -= 1;
}
}
return age;
}
}