IdCardUtil.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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) throws Exception{
  20. String sex = Constant.level_sex_2;
  21. if (CardCode.length() == 18) {
  22. if (Integer.parseInt(CardCode.substring(16).substring(0, 1)) % 2 == 0) {// 判断性别
  23. // modifid by lyr 2016-09-29
  24. // sex = Constant.level_sex_2;
  25. sex = Constant.level_sex_1;
  26. // modifid by lyr 2016-09-29
  27. } else {
  28. // modifid by lyr 2016-09-29
  29. // sex = Constant.level_sex_1;
  30. sex = Constant.level_sex_2;
  31. // modifid by lyr 2016-09-29
  32. }
  33. } else if (CardCode.length() == 15) {
  34. String usex = CardCode.substring(14, 15);// 用户的性别
  35. if (Integer.parseInt(usex) % 2 == 0) {
  36. // sex = Constant.level_sex_2;
  37. sex = Constant.level_sex_1;
  38. } else {
  39. // sex = Constant.level_sex_1;
  40. sex = Constant.level_sex_2;
  41. }
  42. }
  43. return sex;
  44. }
  45. // /**
  46. // * 根据身份证的号码算出当前身份证持有者的年龄
  47. // *
  48. // * @param
  49. // * @throws Exception
  50. // */
  51. // public static int getAgeForIdcard(String card)
  52. // throws Exception {
  53. // int age = 0;
  54. // if (card.length() == 18) {
  55. // String year = card.substring(6).substring(0, 4);// 得到年份
  56. // String yue = card.substring(10).substring(0, 2);// 得到月份
  57. // // String day=CardCode.substring(12).substring(0,2);//得到日
  58. // Date date = new Date();// 得到当前的系统时间
  59. // SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  60. // String fyear = format.format(date).substring(0, 4);// 当前年份
  61. // String fyue = format.format(date).substring(5, 7);// 月份
  62. // if (Integer.parseInt(yue) <= Integer.parseInt(fyue)) { // 当前月份大于用户出身的月份表示已过生
  63. // age = Integer.parseInt(fyear) - Integer.parseInt(year) + 1;
  64. // } else {// 当前用户还没过生
  65. // age = Integer.parseInt(fyear) - Integer.parseInt(year);
  66. // }
  67. // } else if (card.length() == 15) {
  68. // String uyear = "19" + card.substring(6, 8);// 年份
  69. // String uyue = card.substring(8, 10);// 月份
  70. // Date date = new Date();// 得到当前的系统时间
  71. // SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  72. // String fyear = format.format(date).substring(0, 4);// 当前年份
  73. // String fyue = format.format(date).substring(5, 7);// 月份
  74. // // String fday=format.format(date).substring(8,10);
  75. // if (Integer.parseInt(uyue) <= Integer.parseInt(fyue)) { // 当前月份大于用户出身的月份表示已过生
  76. // age = Integer.parseInt(fyear) - Integer.parseInt(uyear) + 1;
  77. // } else {// 当前用户还没过生
  78. // age = Integer.parseInt(fyear) - Integer.parseInt(uyear);
  79. // }
  80. //
  81. // }
  82. // return age;
  83. // }
  84. /**
  85. * 根据身份证的号码算出当前身份证持有者的年龄
  86. *
  87. * @param
  88. * @throws Exception
  89. */
  90. public static int getAgeForIdcard(String idcard) {
  91. int age = 0;
  92. try{
  93. if (StringUtils.isEmpty(idcard)) {
  94. return age;
  95. }
  96. String birth = "";
  97. if (idcard.length() == 18) {
  98. birth = idcard.substring(6, 14);
  99. } else if (idcard.length() == 15) {
  100. birth = "19" + idcard.substring(6, 12);
  101. }
  102. int year = Integer.valueOf(birth.substring(0, 4));
  103. int month = Integer.valueOf(birth.substring(4, 6));
  104. int day = Integer.valueOf(birth.substring(6));
  105. Calendar cal = Calendar.getInstance();
  106. age = cal.get(Calendar.YEAR) - year;
  107. //周岁计算
  108. if (cal.get(Calendar.MONTH) > (month - 1) || (cal.get(Calendar.MONTH) == (month - 1) && cal.get(Calendar.DATE) > day)) {
  109. age--;
  110. }
  111. }catch (Exception e){
  112. return 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. }