IdCardUtil.java 6.0 KB

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