liuxiaolong
2019-05-06 f99bc8c6a1d10610373738edd7d0aa0181c81d99
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package com.cloud.user.utils;
 
import com.cloud.common.utils.DateUtil;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
 
public class PersonCotrollerUtil {
 
 
  // 每位加权因子
  private static int power[] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
 
  /**
   * . 验证日期格式
   * 
   * @param date 日期
   * 
   * @return true false
   */
  public static boolean isDate(String date) {
    try {
      SimpleDateFormat sdf = new SimpleDateFormat(DateUtil.DATE_FMT_3);
      sdf.parse(date);
    } catch (Exception e) {
      return false;
    }
    return true;
 
 
  }
 
  /**
   * . 根据身份证获得性别
   * 
   * @param idCard 身份证号
   * 
   * @return 男 1 女 2
   */
  public static String getGender(String idCard) {
    String genderNum = idCard.substring(idCard.length() - 2, idCard.length() - 1);
    if (Integer.parseInt(genderNum) % 2 == 0) {
      return "2";
    } else {
      return "1";
    }
  }
 
  /**
   * . 身份证验证
   * 
   * @param idcard 身份证号
   * 
   */
  public static boolean isCard2(String idcard){return true;};
  public static boolean isCard(String idcard) {
    // 非18位为假
    if (idcard.length() != 18) {
      return false;
    }
    // 获取前17位
    String idcard17 = idcard.substring(0, 17);
    // 获取第18位
    String idcard18Code = idcard.substring(17, 18);
    char c[] = null;
    String checkCode = "";
    // 是否都为数字
    if (isDigital(idcard17)) {
      c = idcard17.toCharArray();
    } else {
      return false;
    }
 
    if (null != c) {
      int bit[] = new int[idcard17.length()];
 
      bit = converCharToInt(c);
 
      int sum17 = 0;
 
      sum17 = getPowerSum(bit);
 
      // 将和值与11取模得到余数进行校验码判断
      checkCode = getCheckCodeBySum(sum17);
      if (null == checkCode) {
        return false;
      }
      // 将身份证的第18位与算出来的校码进行匹配,不相等就为假
      if (!idcard18Code.equalsIgnoreCase(checkCode)) {
        return false;
      }
    }
    return true;
  }
 
  /**
   * 数字验证
   * 
   * @param str
   * @return
   */
  public static boolean isDigital(String str) {
    return str == null || "".equals(str) ? false : str.matches("^[0-9]*$");
  }
 
 
  /**
   * 将身份证的每位和对应位的加权因子相乘之后,再得到和值
   * 
   * @param bit
   * @return
   */
  public static int getPowerSum(int[] bit) {
 
    int sum = 0;
 
    if (power.length != bit.length) {
      return sum;
    }
 
    for (int i = 0; i < bit.length; i++) {
      for (int j = 0; j < power.length; j++) {
        if (i == j) {
          sum = sum + bit[i] * power[j];
        }
      }
    }
    return sum;
  }
 
  /**
   * 将和值与11取模得到余数进行校验码判断
   * @param sum17
   * @return 校验位
   */
  public static String getCheckCodeBySum(int sum17) {
    String checkCode = null;
    switch (sum17 % 11) {
      case 10:
        checkCode = "2";
        break;
      case 9:
        checkCode = "3";
        break;
      case 8:
        checkCode = "4";
        break;
      case 7:
        checkCode = "5";
        break;
      case 6:
        checkCode = "6";
        break;
      case 5:
        checkCode = "7";
        break;
      case 4:
        checkCode = "8";
        break;
      case 3:
        checkCode = "9";
        break;
      case 2:
        checkCode = "x";
        break;
      case 1:
        checkCode = "0";
        break;
      case 0:
        checkCode = "1";
        break;
    }
    return checkCode;
  }
 
  /**
   * 将字符数组转为整型数组
   * 
   * @param c
   * @return
   * @throws NumberFormatException
   */
  public static int[] converCharToInt(char[] c) throws NumberFormatException {
    int[] a = new int[c.length];
    int k = 0;
    for (char temp : c) {
      a[k++] = Integer.parseInt(String.valueOf(temp));
    }
    return a;
  }
 
 
  /**
   * . 身份证获取生日
   * 
   * @param idCard 身份证号
   * 
   */
  public static Date getBirthday(String idCard) {
    String birthday =
        idCard.substring(6, 10) + "-" + idCard.substring(10, 12) + "-" + idCard.substring(12, 14);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
 
    try {
      return sdf.parse(birthday);
    } catch (ParseException e) {
      e.printStackTrace();
      return null;
    }
  }
 
  /**
   * 手机验证
   * 
   * @param mobile
   * @return
   */
 
  public static boolean isMobile(String mobile) {
    /*String regexMoble = "^((17[0-9])|(14[0-9])|(13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
    return Pattern.matches(regexMoble, mobile);*/
    return true;
  }
 
  /**
   * 电话座机验证
   * 
   * @param phone
   * @return
   */
 
  public static boolean isPhone(String phone) {
   /* String regexPhone = "^(0\\d{2}-\\d{8}(-\\d{1,4})?)|(0\\d{3}-\\d{7,8}(-\\d{1,4})?)$";
    return Pattern.matches(regexPhone, phone);*/
   return true;
  }
 
 
 
}