java 通过身份证计算年龄性别
找了几个网上的博客计算年龄测试用例都通不过,只能自己写了!!!!!
首先要知道身份证有一代和二代,目前几乎都是二代吧
/*** 身份证相关功能计算*/
public class IdCardUtils {/*** 通过身份证计算年龄** @return*/private static int countAge(String idCard) {if(idCard.length() != 18 && idCard.length() != 15){throw new IllegalArgumentException("身份证号长度错误");}String birthTimeString = null;if(idCard.length() == 18){birthTimeString = idCard.substring(6, 10) + "-" + idCard.substring(10, 12) + "-" + idCard.substring(12, 14);}else{birthTimeString = "19" + idCard.substring(6, 8) + "-" + idCard.substring(8, 10) + "-" + idCard.substring(10, 12);}if (birthTimeString == null) {throw new IllegalArgumentException("身份证号长度错误");}// 先截取到字符串中的年、月、日String strs[] = birthTimeString.trim().split("-");int birthYear = Integer.parseInt(strs[0]);int birthMonth = Integer.parseInt(strs[1]);int birthDay = Integer.parseInt(strs[2]);// 得到当前时间的年、月、日LocalDateTime now = LocalDateTime.now();int yearNow = now.getYear();int monthNow = now.getMonthValue();int dayNow = now.getDayOfMonth();// 用当前年月日减去生日年月日int yearMinus = yearNow - birthYear;int monthMinus = monthNow - birthMonth;int dayMinus = dayNow - birthDay;int age = yearMinus-1;if (yearMinus < 0 || age < 0) { //未出生age = 0;return age;}//如果月份大于0说明已经过了生日if (monthMinus > 0) {age++;return age;}//当月出生且出生日大于了当天if (monthMinus == 0 && dayMinus >= 0) {age++;}return age;}/*** 通过身份证计算性别* @param idCard* @return*/public static String getGender(String idCard) {if (idCard.length() != 18 && idCard.length() != 15) {throw new IllegalArgumentException("身份证号长度错误");}Integer gend = null;if (idCard.length() == 18) {gend = Integer.parseInt(String.valueOf(idCard.charAt(idCard.length() - 2)));} else {gend = Integer.parseInt(String.valueOf(idCard.charAt(idCard.length() - 1)));}if (gend % 2 == 1) {return "男";}return "女";}public static void main(String[] args) {int i = countAge("510525202009236850");
// int i = countAge("510525690402670");System.out.println(getGender("510521199707086852"));System.out.println(i);}
java 通过身份证计算年龄性别相关推荐
- JAVA 根据身份证获取年龄
JAVA 根据身份证获取年龄 这里我选用了一条19900407的, 今天的日期是2021/04/10 显示的年龄为31岁. private static int getAge(String idCar ...
- 用java根据身份证号提取性别和年龄
在 Java 中,可以使用以下步骤来提取身份证号中的性别和年龄信息: 使用正则表达式验证身份证号的格式是否正确. 如果身份证号的格式正确,则可以使用截取字符串的方法来提取出生日期信息.身份证号中的出生 ...
- Java根据日期计算年龄——精确到天
最近遇到一个需求,根据孩子的出生日期,计算出孩子当前多大,要求精确到天,如"2岁8个月6天".这里做一下记录,以防以后用到. 以下是博主从简书搜到的一篇代码,奈何找不到原链接了~~ ...
- java 计算年龄 Java 根据出生日期计算年龄
1.把出生日期字符串转换为日期格式. public static Date parse(String strDate) throws ParseException {SimpleDateFormat ...
- Java根据生日计算年龄
//计算年龄private static int getAgeByBirth(Date birthday) {int age = 0;try {Calendar now = Calendar.getI ...
- excel 通过身份证 计算 年龄、性别
=IF(MOD(MID(D3,15,3),2),"男","女") =2022-MID(D3,7,4) 其中D3为身份证所在x.y 坐标
- js校验用户身份证省市区年龄性别
一般正则校验不够严谨,只能比较泛泛,有的只校验长度 常规正则:/^(\d){17}([0-9xX])$/.test(code) // code 身份证号码 本校验可以校验:格式.省市区.年龄.年月日. ...
- java根据出生日期计算年龄_Java 根据出生日期获得年纪
实现步骤: 1.获取当前时间 2.判断出生日期是否小于当前时间,如果大于,则引发一场 3.从当前时间中取出年.月.日:从出生日期中取出年.月.日,年份相减 4.然后做具体判断 示例代码如下: publ ...
- HQL怎么用身份证计算年龄
floor(( UNIX_TIMESTAMP(current_date) -UNIX_TIMESTAMP(substr(card_no,7,8),'yyyyMMdd'))/(365*24*60*60) ...
- java根据生日计算年龄工具类
private static int getAgeByBirth(Date birthday) { int age = 0; try { Cal ...
最新文章
- POJ-2513 Colored Sticks 字典树,欧拉回路
- 信息系统项目管理师案例分析
- CSS基础必备盒模型及清除浮动
- 数据库行转列的sql语句(zt)
- 后端:推荐九款Java 性能优化工具,你都用过吗?
- Mybatis批量更新转
- C语言之理解(*(void (*)())0)()和signal函数
- 挖矿为什么要用显卡_Conflux显卡挖矿收益很高吗?挖矿指南与核算手册
- Written English-书面-一般现在时
- ceph rbd 常用命令使用
- Python使用集合运算检测密码字符串的安全强度
- 学会java要多久_多长时间可以学会Java?
- 选择播放器你需要知道什么?
- git实现ssh免密上传远程gitlab仓库
- 麦克风阵列的同步性、一致性、峰值幅度、RMS、DC偏移
- 2021-07-22MATLAB基于元胞自动机模型的传染病扩散模型
- python绘图画猫咪_使用Python的turtle画小猫咪
- AUTOCAD——点命令
- vue+el国际化-东抄西鉴组合拳
- c++敢死队问题代码(为大一而生)