sql server里 select cast((cast(now as int) - cast(birthdate as int))/365 as char(3) AS age from table ------------------------------------------------------ 我们还年轻牛奶会有的奶牛也会有的 可天天在 csdn 混这些会有吗 ??
//你是不是想要这个 /** * 通过生日得到年龄 * @param java.util.Date * @return int */ public static int getAgeByBirthday(java.util.Date birthday) { int age = 0; GregorianCalendar calendar = new GregorianCalendar(); int curYear = calendar.get(Calendar.YEAR); int curMonth = calendar.get(Calendar.MONTH) + 1; int curDate = calendar.get(Calendar.DATE); if (birthday != null) { calendar.setTime(birthday); int birthYear = calendar.get(Calendar.YEAR); int birthMonth = calendar.get(Calendar.MONTH) + 1; int birthDate = calendar.get(Calendar.DATE); if (curMonth < birthMonth) { age = curYear - birthYear - 1; } else if (curMonth == birthMonth) { if (curDate < birthDate) age = curYear - birthYear - 1; } age = curYear - birthYear; } return age; }
写错了一句 /** * 通过生日得到年龄 * @param java.util.Date * @return int */ public static int getAgeByBirthday(java.util.Date birthday) { int age = 0; GregorianCalendar calendar = new GregorianCalendar(); int curYear = calendar.get(Calendar.YEAR); int curMonth = calendar.get(Calendar.MONTH) + 1; int curDate = calendar.get(Calendar.DATE); if (birthday != null) { calendar.setTime(birthday); int birthYear = calendar.get(Calendar.YEAR); int birthMonth = calendar.get(Calendar.MONTH) + 1; int birthDate = calendar.get(Calendar.DATE); if (curMonth < birthMonth) { age = curYear - birthYear - 1; } else if (curMonth == birthMonth) { if (curDate < birthDate) age = curYear - birthYear - 1; }else{ age = curYear - birthYear; } } return age; }
sql server里 select cast((cast(now as int) - cast(birthdate as int))/365 as char(3) AS age from table ------------------------------------------------------ cast将日期转换成数字型后所得到的数据并不是天数,你必须先取得这个日期的天数后才能这样转换
select cast((cast(now as int) - cast(birthdate as int))/365 as char(3) AS age from table ------------------------------------------------------
我们还年轻牛奶会有的奶牛也会有的
可天天在 csdn 混这些会有吗 ??
我们还年轻牛奶会有的奶牛也会有的
可天天在 csdn 混这些会有吗 ??
/**
* 通过生日得到年龄
* @param java.util.Date
* @return int
*/
public static int getAgeByBirthday(java.util.Date birthday) {
int age = 0;
GregorianCalendar calendar = new GregorianCalendar();
int curYear = calendar.get(Calendar.YEAR);
int curMonth = calendar.get(Calendar.MONTH) + 1;
int curDate = calendar.get(Calendar.DATE);
if (birthday != null) {
calendar.setTime(birthday);
int birthYear = calendar.get(Calendar.YEAR);
int birthMonth = calendar.get(Calendar.MONTH) + 1;
int birthDate = calendar.get(Calendar.DATE);
if (curMonth < birthMonth) {
age = curYear - birthYear - 1;
} else if (curMonth == birthMonth) {
if (curDate < birthDate)
age = curYear - birthYear - 1;
}
age = curYear - birthYear;
}
return age;
}
/**
* 通过生日得到年龄
* @param java.util.Date
* @return int
*/
public static int getAgeByBirthday(java.util.Date birthday) {
int age = 0;
GregorianCalendar calendar = new GregorianCalendar();
int curYear = calendar.get(Calendar.YEAR);
int curMonth = calendar.get(Calendar.MONTH) + 1;
int curDate = calendar.get(Calendar.DATE); if (birthday != null) {
calendar.setTime(birthday);
int birthYear = calendar.get(Calendar.YEAR);
int birthMonth = calendar.get(Calendar.MONTH) + 1;
int birthDate = calendar.get(Calendar.DATE); if (curMonth < birthMonth) {
age = curYear - birthYear - 1;
} else if (curMonth == birthMonth) {
if (curDate < birthDate)
age = curYear - birthYear - 1;
}else{
age = curYear - birthYear;
}
} return age;
}
select cast((cast(now as int) - cast(birthdate as int))/365 as char(3) AS age from table ------------------------------------------------------
cast将日期转换成数字型后所得到的数据并不是天数,你必须先取得这个日期的天数后才能这样转换