CardUtil算出當前身份證持有者的性別和年齡

 

import java.text.SimpleDateFormat;java

import java.util.Date;spa

import java.util.HashMap;orm

import java.util.Map;ci

 

public class CardUtil {     get

         /**string

          * 根據身份證的號碼算出當前身份證持有者的性別和年齡 18位身份證io

          *table

          * @returnform

          * @throws Exceptionclass

          */

         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;

         }

 

}

相關文章
相關標籤/搜索