1 /** 2 * 根据身份证号获取性别 3 * 4 * @param pid 5 * 身份证号 6 * @return 性别 F为女M为男 7 */ 8 public static String getSexByPid(String pid)throws Exception{ 9 if(pid!=null&&(pid.trim().length()== 15||pid.trim().length()==18)){ 10 if(pid.trim().length()==15){ 11 String pid18 = "";//pid15To18(pid); 12 if(Integer.parseInt(pid18.substring(16,17))%2==0){ 13 return "女"; 14 }else{ 15 return "男"; 16 } 17 }else{ 18 if(Integer.parseInt(pid.substring(16,17))%2==0){ 19 return "女"; 20 }else{ 21 return "男"; 22 } 23 } 24 }else{ 25 return null; 26 } 27 } 28 29 30 /** 31 * 15位身份证 转18位 32 * lsp 2017年1月19日13:47:52 33 * @param fifteenIDCard 34 * @return 35 * @throws Exception 36 */ 37 public static String getEighteenIDCard(String fifteenIDCard) throws Exception { 38 if (fifteenIDCard != null && fifteenIDCard.length() == 15) { 39 StringBuilder sb = new StringBuilder(); 40 sb.append(fifteenIDCard.substring(0, 6)).append("19").append( 41 fifteenIDCard.substring(6)); 42 sb.append(getVerifyCode(sb.toString())); 43 return sb.toString(); 44 } else { 45 throw new Exception("不是15位的身份证"); 46 } 47 } 48 49 /** 50 * 获取身份证的校验码 51 * lsp 2017年1月19日13:47:52 52 * @param idCardNumber 53 * @return 54 * @throws Exception 55 */ 56 public static char getVerifyCode(String idCardNumber) throws Exception { 57 if (idCardNumber == null || idCardNumber.length() < 17) { 58 throw new Exception("不合法的身份证号码"); 59 } 60 char[] Ai = idCardNumber.toCharArray(); 61 int[] Wi = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 }; 62 char[] verifyCode = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', 63 '2' }; 64 int S = 0; 65 int Y; 66 for (int i = 0; i < Wi.length; i++) { 67 S += (Ai[i] - '0') * Wi[i]; 68 } 69 Y = S % 11; 70 return verifyCode[Y]; 71 } 72 73 public static void main(String[] args) throws Exception { 74 System.out.println(getEighteenIDCard("330324360802695")); 75 }