java身份证计算年龄
2024-08-23 06:06:22
技术交流群: 233513714
/**
* 根据身份证计算年龄
*
* @param idcard
* @return
*/
public static Integer idCardToAge(String idcard) {
Integer selectYear = Integer.valueOf(idcard.substring(6, 10)); //出生的年份
Integer selectMonth = Integer.valueOf(idcard.substring(10, 12)); //出生的月份
Integer selectDay = Integer.valueOf(idcard.substring(12, 14)); //出生的日期
Calendar cal = Calendar.getInstance();
Integer yearMinus = cal.get(Calendar.YEAR) - selectYear;
Integer monthMinus = cal.get(Calendar.MONTH) + 1 - selectMonth;
Integer dayMinus = cal.get(Calendar.DATE) - selectDay;
Integer age = yearMinus;
if (yearMinus < 0) {
age = 0;
} else if (yearMinus == 0) {
age = 0;
} else if (yearMinus > 0) {
if (monthMinus == 0) {
if (dayMinus < 0) {
age = age - 1;
}
} else if (monthMinus > 0) {
age = age + 1;
}
}
return age;
}
最新文章
- Spring 下默认事务机制中@Transactional 无效的原因
- DbHelper为什么要用Using?
- Bellman_Ford
- 爱默生UPS并机系统:进入与退出操作方法
- 使用cocos2d-x 3.2下载图片资源小例子
- JAVA环境变量JAVA_HOME、CLASSPATH、PATH设置详解
- Hadoop HA的搭建
- mongodb学习(翻译1)
- mybatis深入理解之 # 与 $ 区别以及 sql 预编译
- Suricata默认规则集相关
- get.go
- 如何搭建SVN的客户端和使用
- 使用ThreadPoolExecutor进行多线程编程
- 微信公众号授权,支付,退款总结【shoucang】
- MyBatis笔记----MyBatis 入门经典的两个例子: XML 定义与注解定义
- GeoServer java.io.IOException: No such resource: generic.sld No such resource: generic.sld
- JVM内容梳理
- (11)学习笔记 ) ASP.NET CORE微服务 Micro-Service ---- Thrift高效通讯 (完结)
- POJ2516 Minimum Cost【最小费用最大流】
- 3D Slicer 4.7.0 VS 2010 Compile 编译
热门文章
- 【转载】每天一个Linux命令
- CopyTranslator-复制即翻译的外文辅助阅读翻译解决方案
- Python-Django框架学习笔记——第一课:Hello World
- Linux Mint,Ubuntu 18 ,Deepin15.7 安装mysql 没有提示输入密码,修改root用户密码过程
- vuejs使用组件的细节点
- 2017.10.24 Java 详解 JVM 工作原理和流程
- 2017.9.26 request请求参数用法
- MyBatis简单了解
- IE 8 下小心使用console.log()
- WEB相关背景知识(新手)