用身份证获取性别与年龄

 

---------通过身份证判断性别

 

create or replace function sex(id in varchar)return varchar
as
SFZHM varchar(50);
begin
  if length(id)=18 then
    select decode(mod(to_number(substr(id,17, 1)),2),0, '女',1,'男') into SFZHM from dual;
  elsif length(id)=15 then
    select decode(mod(to_number(substr(id,15, 1)),2),0, '女',1,'男') into SFZHM from dual;
  else
    select '未知' into SFZHM from dual;
  end if;
  return SFZHM;
end;

 

----------通过身份证判断年龄

create or replace function age(id in varchar)return varchar
as
SFZHM varchar(50);
begin
    if length(id)=18 and to_number(substr(id,11,2))<13
     and to_number(substr(id,13,2))<31 then
    select  floor(months_between(SYSDATE,to_date(substr(id,7,8),'yyyymmdd'))/12) into SFZHM from dual;
   else
     select 0 into SFZHM from dual;
    end if;
  return SFZHM;
end;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Java中的SimpleDateFormat类和Calendar类来实现按照身份证获取年龄、月份、天数和性别的功能。具体实现方法可以参考以下代码: ```java import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class IdCardUtil { /** * 根据身份证获取年龄、月份、天数和性别 * @param idCard 身份证号 * @return 返回一个数组,依次为年龄、月份、天数和性别 */ public static int[] getAgeMonthDaySexByIdCard(String idCard) { int[] result = new int[4]; if (idCard == null || idCard.length() != 18) { return result; } String birthdayStr = idCard.substring(6, 14); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); Date birthday = null; try { birthday = sdf.parse(birthdayStr); } catch (Exception e) { e.printStackTrace(); } if (birthday == null) { return result; } Calendar calendar = Calendar.getInstance(); calendar.setTime(birthday); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH) + 1; int day = calendar.get(Calendar.DAY_OF_MONTH); result[0] = getAge(year, month, day); result[1] = month; result[2] = day; result[3] = getSex(idCard); return result; } /** * 根据出生年月日计算年龄 * @param year 出生年份 * @param month 出生月份 * @param day 出生日期 * @return 返回年龄 */ private static int getAge(int year, int month, int day) { Calendar calendar = Calendar.getInstance(); int nowYear = calendar.get(Calendar.YEAR); int nowMonth = calendar.get(Calendar.MONTH) + 1; int nowDay = calendar.get(Calendar.DAY_OF_MONTH); int age = nowYear - year; if (nowMonth < month || (nowMonth == month && nowDay < day)) { age--; } return age; } /** * 根据身份证获取性别 * @param idCard 身份证号 * @return 返回性别,1表示男性,2表示女性 */ private static int getSex(String idCard) { int sex = 0; if (idCard.length() == 15) { sex = idCard.charAt(14) - '0'; } else if (idCard.length() == 18) { sex = idCard.charAt(16) - '0'; } if (sex % 2 == 0) { return 2; } else { return 1; } } } ``` 使用示例: ```java public class Test { public static void main(String[] args) { String idCard = "110101199003077890"; int[] result = IdCardUtil.getAgeMonthDaySexByIdCard(idCard); System.out.println("年龄:" + result[0]); System.out.println("月份:" + result[1]); System.out.println("天数:" + result[2]); System.out.println("性别:" + result[3]); } } ``` 输出结果: ``` 年龄:31 月份:3 天数:7 性别:1 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值