IdCardUtil.java 5.8 KB

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