IdCardUtil.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package com.yihu.wlyy.util;
  2. import com.yihu.wlyy.job.Constant;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Date;
  5. import java.util.HashMap;
  6. import java.util.Map;
  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. if (CardCode.length() == 18) {
  23. if (Integer.parseInt(CardCode.substring(16).substring(0, 1)) % 2 == 0) {// 判断性别
  24. // modifid by lyr 2016-09-29
  25. // sex = Constant.level_sex_2;
  26. sex = Constant.level_sex_1;
  27. // modifid by lyr 2016-09-29
  28. } else {
  29. // modifid by lyr 2016-09-29
  30. // sex = Constant.level_sex_1;
  31. sex = Constant.level_sex_2;
  32. // modifid by lyr 2016-09-29
  33. }
  34. } else if (CardCode.length() == 15) {
  35. String usex = CardCode.substring(14, 15);// 用户的性别
  36. if (Integer.parseInt(usex) % 2 == 0) {
  37. // sex = Constant.level_sex_2;
  38. sex = Constant.level_sex_1;
  39. } else {
  40. // sex = Constant.level_sex_1;
  41. sex = Constant.level_sex_2;
  42. }
  43. }
  44. return sex;
  45. }
  46. /**
  47. * 根据身份证的号码算出当前身份证持有者的年龄
  48. *
  49. * @param
  50. * @throws Exception
  51. */
  52. public static int getAgeForIdcard(String card)
  53. throws Exception {
  54. int age = 0;
  55. if (card.length() == 18) {
  56. String year = card.substring(6).substring(0, 4);// 得到年份
  57. String yue = card.substring(10).substring(0, 2);// 得到月份
  58. // String day=CardCode.substring(12).substring(0,2);//得到日
  59. Date date = new Date();// 得到当前的系统时间
  60. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  61. String fyear = format.format(date).substring(0, 4);// 当前年份
  62. String fyue = format.format(date).substring(5, 7);// 月份
  63. if (Integer.parseInt(yue) <= Integer.parseInt(fyue)) { // 当前月份大于用户出身的月份表示已过生
  64. age = Integer.parseInt(fyear) - Integer.parseInt(year) + 1;
  65. } else {// 当前用户还没过生
  66. age = Integer.parseInt(fyear) - Integer.parseInt(year);
  67. }
  68. } else if (card.length() == 15) {
  69. String uyear = "19" + card.substring(6, 8);// 年份
  70. String uyue = card.substring(8, 10);// 月份
  71. Date date = new Date();// 得到当前的系统时间
  72. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  73. String fyear = format.format(date).substring(0, 4);// 当前年份
  74. String fyue = format.format(date).substring(5, 7);// 月份
  75. // String fday=format.format(date).substring(8,10);
  76. if (Integer.parseInt(uyue) <= Integer.parseInt(fyue)) { // 当前月份大于用户出身的月份表示已过生
  77. age = Integer.parseInt(fyear) - Integer.parseInt(uyear) + 1;
  78. } else {// 当前用户还没过生
  79. age = Integer.parseInt(fyear) - Integer.parseInt(uyear);
  80. }
  81. }
  82. return age;
  83. }
  84. /**
  85. * 身份证提取出身日期
  86. * @param card
  87. * @return
  88. * @throws Exception
  89. */
  90. public static Date getBirthdayForIdcard(String card)
  91. throws Exception {
  92. Date b = null;
  93. if (card.length() == 18) {
  94. String year = card.substring(6).substring(0, 4);// 得到年份
  95. String yue = card.substring(10).substring(0, 2);// 得到月份
  96. String ri = card.substring(12).substring(0, 2);// 得到日
  97. // String day=CardCode.substring(12).substring(0,2);//得到日
  98. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  99. b=format.parse(year+"-"+yue+"-"+ri);
  100. } else if (card.length() == 15) {
  101. String uyear = "19" + card.substring(6, 8);// 年份
  102. String uyue = card.substring(8, 10);// 月份
  103. String uri = card.substring(10, 12);// 得到日
  104. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  105. b=format.parse(uyear+"-"+uyue+"-"+uri);
  106. }
  107. return b;
  108. }
  109. public static void main(String[] args) throws Exception {
  110. System.out.println(getSexForIdcard("350206199109092018"));
  111. }
  112. }