算年龄

1
[color=olive]
SELECT
name_sei ||' '||name_mei as staff_NM,
ten_cd,
staff_cd,
birthday,
case sex when '1' then '男性' else '女性' end as strSex,
(TO_DATE(TO_CHAR(NOW(),'YYYY/MM/DD') ,'YYYY/MM/DD') -BIRTHDAY)/365 AS AGE, //直接算
TO_CHAR(BIRTHDAY,'yyyy-MM-dd') AS BIRTHDAY
FROM
t_staff
WHERE
staff_cd = '999999'
[/color]
2

//年齢=生年月日より、年齢を算出する

年齢=TODAY()(DBから取得する)の年

生年月日の月≦ TODAY()の月日の場合、
年齢 = 上で取得した年齢
生年月日の月 > TODAY()の月日の場合、
年齢 = 上で取得した年齢




//年齢=生年月日より、年齢を算出する
String birthday=delStrNulls(rs.getString("BIRTHDAY"));
int age=0;
int month=0;
int day=0;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//システムの年、月、日
String cYear = sdf.format(new Date()).substring(0,4);
String cmonth = sdf.format(new Date()).substring(5,7);
String cday = sdf.format(new Date()).substring(8,10);
// 誕生日のシステムの年、月、日
if(birthday.length()>0){
String bYear = birthday.substring(0,4);
String bmonth = birthday.substring(5,7);
String bday = birthday.substring(8,10);
month=Integer.parseInt(bmonth);
day=Integer.parseInt(bday);
int year= Integer.parseInt(cYear) - Integer.parseInt(bYear);
if(month<Integer.parseInt(cmonth)){
age=year;
}else if(month==Integer.parseInt(cmonth)){
if(day<=Integer.parseInt(cday)){
age=year;
}else{
age=year-1;
}
}
else{
age=year-1;
}
}
if(age!=0){
vo.setAge(age+"");
}else{
vo.setAge("");
}


使用Python计算年龄,可通过将当前日期与出生日期进行比较来实现。以下提供几种不同的实现方式: **方法一**: ```python import datetime def calculate_age(birth_date): # 获取当前日期 current_date = datetime.date.today() # 计算年龄 age = current_date.year - birth_date.year # 检查是否已经过了生日 if current_date.month < birth_date.month or (current_date.month == birth_date.month and current_date.day < birth_date.day): age -= 1 return age # 测试示例 birth_date = datetime.date(1990, 5, 15) age = calculate_age(birth_date) print("年龄:", age) ``` 该方法定义了`calculate_age`函数,接受一个`birth_date`参数(`datetime.date`对象,表示出生日期)。使用`datetime.date.today()`获取当前日期,计算当前年份与出生年份的差异得到初始年龄。接着检查当前月份和日期是否已过出生日期,若未过则年龄减1,最后返回计算得到的年龄 [^2]。 **方法二**: ```python import datetime sName = input('请输入您的名字:') birthday_year = int(input('请输入您的出生年份:')) birthday_month = int(input('请输入您的出生月份:')) birthday_day = int(input('请输入您的出生日期:')) today = datetime.date.today() current_year = today.year this_year_birthday = datetime.date(current_year, birthday_month, birthday_day) if today < this_year_birthday: age = current_year - birthday_year - 1 next_birthday_days = (this_year_birthday - today).days print(f'您好,{sName},您今年的年龄是{age}。您的生日还有{next_birthday_days}天。') elif today == this_year_birthday: age = current_year - birthday_year print(f'生日快乐,{sName}! 您今年{age}岁了。') else: age = current_year - birthday_year days_since_birthday = (today - this_year_birthday).days print(f'您好,{sName},您今年的年龄是{age}。您的生日已经过去{days_since_birthday}天了。') ``` 此方法通过用户输入姓名、出生年份、月份和日期,根据当前日期与当年生日日期的比较结果,计算年龄以及距离生日的天数或生日已过去的天数,并输出相应信息 [^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值