IdCardUtil.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package com.yihu.wlyy.util;
  2. import com.yihu.wlyy.job.Constant;
  3. import org.apache.commons.lang3.StringUtils;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Calendar;
  6. import java.util.Date;
  7. import java.util.HashMap;
  8. import java.util.Map;
  9. import java.util.concurrent.ExecutionException;
  10. /**
  11. * Created by Administrator on 2016.08.17.
  12. * 身份证工具类
  13. */
  14. public class IdCardUtil {
  15. // /**
  16. // * 根据身份证的号码算出当前身份证持有者的年龄
  17. // *
  18. // * @param
  19. // * @throws Exception
  20. // */
  21. // public static int getAgeForIdcard(String card)
  22. // throws Exception {
  23. // int age = 0;
  24. // if (card.length() == 18) {
  25. // String year = card.substring(6).substring(0, 4);// 得到年份
  26. // String yue = card.substring(10).substring(0, 2);// 得到月份
  27. // // String day=CardCode.substring(12).substring(0,2);//得到日
  28. // Date date = new Date();// 得到当前的系统时间
  29. // SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  30. // String fyear = format.format(date).substring(0, 4);// 当前年份
  31. // String fyue = format.format(date).substring(5, 7);// 月份
  32. // if (Integer.parseInt(yue) <= Integer.parseInt(fyue)) { // 当前月份大于用户出身的月份表示已过生
  33. // age = Integer.parseInt(fyear) - Integer.parseInt(year) + 1;
  34. // } else {// 当前用户还没过生
  35. // age = Integer.parseInt(fyear) - Integer.parseInt(year);
  36. // }
  37. // } else if (card.length() == 15) {
  38. // String uyear = "19" + card.substring(6, 8);// 年份
  39. // String uyue = card.substring(8, 10);// 月份
  40. // Date date = new Date();// 得到当前的系统时间
  41. // SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  42. // String fyear = format.format(date).substring(0, 4);// 当前年份
  43. // String fyue = format.format(date).substring(5, 7);// 月份
  44. // // String fday=format.format(date).substring(8,10);
  45. // if (Integer.parseInt(uyue) <= Integer.parseInt(fyue)) { // 当前月份大于用户出身的月份表示已过生
  46. // age = Integer.parseInt(fyear) - Integer.parseInt(uyear) + 1;
  47. // } else {// 当前用户还没过生
  48. // age = Integer.parseInt(fyear) - Integer.parseInt(uyear);
  49. // }
  50. //
  51. // }
  52. // return age;
  53. // }
  54. /**
  55. * 根据身份证的号码算出当前身份证持有者的年龄
  56. *
  57. * @param
  58. * @throws Exception
  59. */
  60. public static int getAgeForIdcard(String idcard) {
  61. int age = 0;
  62. if (StringUtils.isEmpty(idcard)) {
  63. return age;
  64. }
  65. String birth = "";
  66. if (idcard.length() == 18) {
  67. birth = idcard.substring(6, 14);
  68. } else if (idcard.length() == 15) {
  69. birth = "19" + idcard.substring(6, 12);
  70. }
  71. int year = Integer.valueOf(birth.substring(0, 4));
  72. int month = Integer.valueOf(birth.substring(4, 6));
  73. int day = Integer.valueOf(birth.substring(6));
  74. Calendar cal = Calendar.getInstance();
  75. age = cal.get(Calendar.YEAR) - year;
  76. //周岁计算
  77. if (cal.get(Calendar.MONTH) > (month - 1) || (cal.get(Calendar.MONTH) == (month - 1) && cal.get(Calendar.DATE) < day)) {
  78. age--;
  79. }
  80. return age;
  81. }
  82. /**
  83. * 身份证提取出身日期
  84. *
  85. * @param card
  86. * @return
  87. * @throws Exception
  88. */
  89. public static Date getBirthdayForIdcard(String card)
  90. throws Exception {
  91. Date b = null;
  92. if (card.length() == 18) {
  93. String year = card.substring(6).substring(0, 4);// 得到年份
  94. String yue = card.substring(10).substring(0, 2);// 得到月份
  95. String ri = card.substring(12).substring(0, 2);// 得到日
  96. // String day=CardCode.substring(12).substring(0,2);//得到日
  97. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  98. b = format.parse(year + "-" + yue + "-" + ri);
  99. } else if (card.length() == 15) {
  100. String uyear = "19" + card.substring(6, 8);// 年份
  101. String uyue = card.substring(8, 10);// 月份
  102. String uri = card.substring(10, 12);// 得到日
  103. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  104. b = format.parse(uyear + "-" + uyue + "-" + uri);
  105. }
  106. return b;
  107. }
  108. /**
  109. * 根据身份证的号码算出当前身份证持有者的性别
  110. * 1 女 2 男 3未知
  111. *
  112. * @return
  113. * @throws Exception
  114. */
  115. public static String getSexForIdcard(String CardCode)
  116. throws Exception {
  117. String sex = Constant.level_sex_3;
  118. if (CardCode.length() == 18) {
  119. if (Integer.parseInt(CardCode.substring(16).substring(0, 1)) % 2 == 0) {// 判断性别
  120. // modifid by lyr 2016-09-29
  121. // sex = Constant.level_sex_2;
  122. sex = Constant.level_sex_1;
  123. // modifid by lyr 2016-09-29
  124. } else {
  125. // modifid by lyr 2016-09-29
  126. // sex = Constant.level_sex_1;
  127. sex = Constant.level_sex_2;
  128. // modifid by lyr 2016-09-29
  129. }
  130. } else if (CardCode.length() == 15) {
  131. String usex = CardCode.substring(14, 15);// 用户的性别
  132. if (Integer.parseInt(usex) % 2 == 0) {
  133. // sex = Constant.level_sex_2;
  134. sex = Constant.level_sex_1;
  135. } else {
  136. // sex = Constant.level_sex_1;
  137. sex = Constant.level_sex_2;
  138. }
  139. }
  140. return sex;
  141. }
  142. }