Java工具类

Java的身份证号码工具类

 

网上看到的一段代码:


001 /**
002   * Copyright (C) 2009-2010 Yichuan, Fuchun All rights reserved.
003   * Licensed to the Apache Software Foundation (ASF) under one or more
004   * contributor license agreements. See the NOTICE file distributed with
005   * this work for additional information regarding copyright ownership.
006   * The ASF licenses this file to You under the Apache License, Version 2.0
007   * (the "License"); you may not use this file except in compliance with
008   * the License. You may obtain a copy of the License at
009   * http://www.apache.org/licenses/LICENSE-2.0
010   * Unless required by applicable law or agreed to in writing, software
011   * distributed under the License is distributed on an "AS IS" BASIS,
012   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013   * See the License for the specific language governing permissions and
014   * limitations under the License.
015   * @(#) IdcardUtils.java Date: 2010-06-17
016   */
017 package my.tools;
018  
019 import java.text.ParseException;
020 import java.text.SimpleDateFormat;
021 import java.util.Calendar;
022 import java.util.Date;
023 import java.util.HashMap;
024 import java.util.Map;
025  
026 /**
027   * 身份证工具类
028   *
029   * @author June
030   * @version 1.0, 2010-06-17
031   */
032 public class IdcardUtils extends StringUtils {
033  
034      /** 中国公民身份证号码最小长度。 */
035      public static final int CHINA_ID_MIN_LENGTH = 15 ;
036  
037      /** 中国公民身份证号码最大长度。 */
038      public static final int CHINA_ID_MAX_LENGTH = 18 ;
039  
040      /** 省、直辖市代码表 */
041      public static final String cityCode[] = {
042              "11" , "12" , "13" , "14" , "15" , "21" , "22" , "23" , "31" , "32" , "33" , "34" , "35" , "36" , "37" , "41" ,
043              "42" , "43" , "44" , "45" , "46" , "50" , "51" , "52" , "53" , "54" , "61" , "62" , "63" , "64" , "65" , "71" ,
044              "81" , "82" , "91"
045      };
046  
047      /** 每位加权因子 */
048      public static final int power[] = {
049              7 , 9 , 10 , 5 , 8 , 4 , 2 , 1 , 6 , 3 , 7 , 9 , 10 , 5 , 8 , 4 , 2
050      };
051  
052      /** 第18位校检码 */
053      public static final String verifyCode[] = {
054              "1" , "0" , "X" , "9" , "8" , "7" , "6" , "5" , "4" , "3" , "2"
055      };
056      /** 最低年限 */
057      public static final int MIN = 1930 ;
058      public static Map<String, String> cityCodes = new HashMap<String, String>();
059      /** 台湾身份首字母对应数字 */
060      public static Map<String, Integer> twFirstCode = new HashMap<String, Integer>();
061      /** 香港身份首字母对应数字 */
062      public static Map<String, Integer> hkFirstCode = new HashMap<String, Integer>();
063      static {
064          cityCodes.put( "11" , "北京" );
065          cityCodes.put( "12" , "天津" );
066          cityCodes.put( "13" , "河北" );
067          cityCodes.put( "14" , "山西" );
068          cityCodes.put( "15" , "内蒙古" );
069          cityCodes.put( "21" , "辽宁" );
070          cityCodes.put( "22" , "吉林" );
071          cityCodes.put( "23" , "黑龙江" );
072          cityCodes.put( "31" , "上海" );
073          cityCodes.put( "32" , "江苏" );
074          cityCodes.put( "33" , "浙江" );
075          cityCodes.put( "34" , "安徽" );
076          cityCodes.put( "35" , "福建" );
077          cityCodes.put( "36" , "江西" );
078          cityCodes.put( "37" , "山东" );
079          cityCodes.put( "41" , "河南" );
080          cityCodes.put( "42" , "湖北" );
081          cityCodes.put( "43" , "湖南" );
082          cityCodes.put( "44" , "广东" );
083          cityCodes.put( "45" , "广西" );
084          cityCodes.put( "46" , "海南" );
085          cityCodes.put( "50" , "重庆" );
086          cityCodes.put( "51" , "四川" );
087          cityCodes.put( "52" , "贵州" );
088          cityCodes.put( "53" , "云南" );
089          cityCodes.put( "54" , "西藏" );
090          cityCodes.put( "61" , "陕西" );
091          cityCodes.put( "62" , "甘肃" );
092          cityCodes.put( "63" , "青海" );
093          cityCodes.put( "64" , "宁夏" );
094          cityCodes.put( "65" , "新疆" );
095          cityCodes.put( "71" , "台湾" );
096          cityCodes.put( "81" , "香港" );
097          cityCodes.put( "82" , "澳门" );
098          cityCodes.put( "91" , "国外" );
099          twFirstCode.put( "A" , 10 );
100          twFirstCode.put( "B" , 11 );
101          twFirstCode.put( "C" , 12 );
102          twFirstCode.put( "D" , 13 );
103          twFirstCode.put( "E" , 14 );
104          twFirstCode.put( "F" , 15 );
105          twFirstCode.put( "G" , 16 );
106          twFirstCode.put( "H" , 17 );
107          twFirstCode.put( "J" , 18 );
108          twFirstCode.put( "K" , 19 );
109          twFirstCode.put( "L" , 20 );
110          twFirstCode.put( "M" , 21 );
111          twFirstCode.put( "N" , 22 );
112          twFirstCode.put( "P" , 23 );
113          twFirstCode.put( "Q" , 24 );
114          twFirstCode.put( "R" , 25 );
115          twFirstCode.put( "S" , 26 );
116          twFirstCode.put( "T" , 27 );
117          twFirstCode.put( "U" , 28 );
118          twFirstCode.put( "V" , 29 );
119          twFirstCode.put( "X" , 30 );
120          twFirstCode.put( "Y" , 31 );
121          twFirstCode.put( "W" , 32 );
122          twFirstCode.put( "Z" , 33 );
123          twFirstCode.put( "I" , 34 );
124          twFirstCode.put( "O" , 35 );
125          hkFirstCode.put( "A" , 1 );
126          hkFirstCode.put( "B" , 2 );
127          hkFirstCode.put( "C" , 3 );
128          hkFirstCode.put( "R" , 18 );
129          hkFirstCode.put( "U" , 21 );
130          hkFirstCode.put( "Z" , 26 );
131          hkFirstCode.put( "X" , 24 );
132          hkFirstCode.put( "W" , 23 );
133          hkFirstCode.put( "O" , 15 );
134          hkFirstCode.put( "N" , 14 );
135      }
136  
137      /**
138       * 将15位身份证号码转换为18位
139       *
140       * @param idCard
141       *            15位身份编码
142       * @return 18位身份编码
143       */
144      public static String conver15CardTo18(String idCard) {
145          String idCard18 = "" ;
146          if (idCard.length() != CHINA_ID_MIN_LENGTH) {
147              return null ;
148          }
149          if (isNum(idCard)) {
150              // 获取出生年月日
151              String birthday = idCard.substring( 6 , 12 );
152              Date birthDate = null ;
153              try {
154                  birthDate = new SimpleDateFormat( "yyMMdd" ).parse(birthday);
155              } catch (ParseException e) {
156                  e.printStackTrace();
157              }
158              Calendar cal = Calendar.getInstance();
159              if (birthDate != null )
160                  cal.setTime(birthDate);
161              // 获取出生年(完全表现形式,如:2010)
162              String sYear = String.valueOf(cal.get(Calendar.YEAR));
163              idCard18 = idCard.substring( 0 , 6 ) + sYear + idCard.substring( 8 );
164              // 转换字符数组
165              char [] cArr = idCard18.toCharArray();
166              if (cArr != null ) {
167                  int [] iCard = converCharToInt(cArr);
168                  int iSum17 = getPowerSum(iCard);
169                  // 获取校验位
170                  String sVal = getCheckCode18(iSum17);
171                  if (sVal.length() > 0 ) {
172                      idCard18 += sVal;
173                  } else {
174                      return null ;
175                  }
176              }
177          } else {
178              return null ;
179          }
180          return idCard18;
181      }
182  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值