---
title: "算年龄"
author: "salute"
date: "2010-11-12T09:52:55Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2010-11-12T09:55:18Z"
id: 809487
url: "https://salute.iteye.com/blog/809487"
---

1
<span style="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'
</span>
2

//年齢=生年月日より、年齢を算出する

年齢＝TODAY（）（DBから取得する）の年					
								
生年月日の月≦	TODAY（）の月日の場合、		
	年齢		＝	上で取得した年齢				
生年月日の月	＞	TODAY（）の月日の場合、		
	年齢		＝	上で取得した年齢				


```java



//年齢=生年月日より、年齢を算出する
					 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("");	
					}



```