CardUtil算出当前身份证持有者的性别和年龄

 

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.HashMap;

import java.util.Map;

 

public class CardUtil {     

         /**

          * 根据身份证的号码算出当前身份证持有者的性别和年龄 18位身份证

          *

          * @return

          * @throws Exception

          */

         public static Map<String, Object> getCarInfo(String CardCode)

                            throws Exception {

                   Map<String, Object> map = new HashMap<String, Object>();

                   String year = CardCode.substring(6).substring(0, 4);// 得到年份

                   String yue = CardCode.substring(10).substring(0, 2);// 得到月份

                   // String day=CardCode.substring(12).substring(0,2);//得到日

                   Integer sex;

                   if (Integer.parseInt(CardCode.substring(16).substring(0, 1)) % 2 == 0) {// 判断性别

                            sex =1; //"女";

                   } else {

                            sex =0; //"男";

                   }

                   Date date = new Date();// 得到当前的系统时间

                   SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

                   String fyear = format.format(date).substring(0, 4);// 当前年份

                   String fyue = format.format(date).substring(5, 7);// 月份

                   // String fday=format.format(date).substring(8,10);

                   int age = 0;

                   if (Integer.parseInt(yue) <= Integer.parseInt(fyue)) { // 当前月份大于用户出身的月份表示已过生

                            age = Integer.parseInt(fyear) - Integer.parseInt(year) + 1;

                   } else {// 当前用户还没过生

                            age = Integer.parseInt(fyear) - Integer.parseInt(year);

                   }

                   map.put("sex", sex);

                   map.put("age", age);

                   return map;

         }

 

         /**

          * 15位身份证的验证

          *

          * @param

          * @throws Exception

          */

         public static Map<String, Object> getCarInfo15W(String card)throws Exception {

                   Map<String, Object> map = new HashMap<String, Object>();

                   String uyear = "19" + card.substring(6, 8);// 年份

                   String uyue = card.substring(8, 10);// 月份

                   // String uday=card.substring(10, 12);//日

                   String usex = card.substring(14, 15);// 用户的性别

                   String sex;

                   if (Integer.parseInt(usex) % 2 == 0) {

                            sex = "女";

                   } else {

                            sex = "男";

                   }

                   Date date = new Date();// 得到当前的系统时间

                   SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

                   String fyear = format.format(date).substring(0, 4);// 当前年份

                   String fyue = format.format(date).substring(5, 7);// 月份

                   // String fday=format.format(date).substring(8,10);

                   int age = 0;

                   if (Integer.parseInt(uyue) <= Integer.parseInt(fyue)) { // 当前月份大于用户出身的月份表示已过生

                            age = Integer.parseInt(fyear) - Integer.parseInt(uyear) + 1;

                   } else {// 当前用户还没过生

                            age = Integer.parseInt(fyear) - Integer.parseInt(uyear);

                   }

                   map.put("sex", sex);

                   map.put("age", age);

                   return map;

         }

 

}

posted on   我是司  阅读(388)  评论(0编辑  收藏  举报

编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示