1. 程式人生 > >在Service中獲取年齡的代碼

在Service中獲取年齡的代碼

amp EDA form lis student imp sys 獲取 RM

//java.sql.Date,java.util.Date-->>存儲日期數據

//SimpleDateFormat-->>字符串變日期,日期變 字符串

//SimpleDateFormat-->>字符串變日期,日期變 字符串

Calendar calendar=Calendar.getInstance();//存儲當前日期

int year_now=calendar.get(Calendar.YEAR);

int month_now=calendar.get(Calendar.MONTH)+1;

int day_now=calendar.get(Calendar.DAY_OF_MONTH);

System.out.println(year_now);

System.out.println(month_now);

System.out.println(day_now);

for(Student stu:stulist){

calendar.setTime(stu.getBirthday());

int year_pass=calendar.get(Calendar.YEAR);

int month_pass=calendar.get(Calendar.MONTH)+1;

int day_pass=calendar.get(Calendar.DAY_OF_MONTH);

int age=year_now-year_pass;

if(month_now<month_pass){

age--;

}else if((month_now==month_pass)&&(day_now<day_pass))

{

age--;

}

stu.setAge(age);

}

在Service中獲取年齡的代碼