IdcardUtil.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package com.yihu.ehr.analysis.etl.util;
  2. import org.apache.commons.lang3.StringUtils;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Calendar;
  5. import java.util.Date;
  6. /**
  7. * 身份证号工具类
  8. *
  9. * Created by lyr-pc on 2017/2/17.
  10. */
  11. public class IdcardUtil {
  12. /**
  13. * 根据身份证的号码算出当前身份证持有者的年龄
  14. *
  15. * @param
  16. * @throws Exception
  17. */
  18. public static int getAgeFromIdcard(String idcard) {
  19. try {
  20. int age = -1;
  21. if (StringUtils.isEmpty(idcard)) {
  22. return age;
  23. }
  24. String birth = "";
  25. if (idcard.length() == 18) {
  26. birth = idcard.substring(6, 14);
  27. } else if (idcard.length() == 15) {
  28. birth = "19" + idcard.substring(6, 12);
  29. }
  30. int year = Integer.valueOf(birth.substring(0, 4));
  31. int month = Integer.valueOf(birth.substring(4, 6));
  32. int day = Integer.valueOf(birth.substring(6));
  33. Calendar cal = Calendar.getInstance();
  34. age = cal.get(Calendar.YEAR) - year;
  35. //周岁计算
  36. if (cal.get(Calendar.MONTH) < (month - 1) || (cal.get(Calendar.MONTH) == (month - 1) && cal.get(Calendar.DATE) < day)) {
  37. age--;
  38. }
  39. return age;
  40. } catch (Exception e) {
  41. return -1;
  42. }
  43. }
  44. /**
  45. * 根据身份证的号码算出当前身份证持有者的性别
  46. * 1 男 2 女 3未知
  47. *
  48. * @return
  49. * @throws Exception
  50. */
  51. public static int getSexForIdcard(String CardCode)
  52. throws Exception {
  53. int sex = 3;
  54. if (CardCode.length() == 18) {
  55. if (Integer.parseInt(CardCode.substring(16).substring(0, 1)) % 2 == 0) {// 判断性别
  56. sex = 2;
  57. } else {
  58. sex = 1;
  59. }
  60. } else if (CardCode.length() == 15) {
  61. String usex = CardCode.substring(14, 15);// 用户的性别
  62. if (Integer.parseInt(usex) % 2 == 0) {
  63. sex = 2;
  64. } else {
  65. sex = 1;
  66. }
  67. }
  68. return sex;
  69. }
  70. /**
  71. * 身份证提取出身日期
  72. *
  73. * @param card
  74. * @return
  75. * @throws Exception
  76. */
  77. public static Date getBirthdayForIdcard(String card)
  78. throws Exception {
  79. Date b = null;
  80. if (card.length() == 18) {
  81. String year = card.substring(6).substring(0, 4);// 得到年份
  82. String yue = card.substring(10).substring(0, 2);// 得到月份
  83. String ri = card.substring(12).substring(0, 2);// 得到日
  84. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  85. b = format.parse(year + "-" + yue + "-" + ri);
  86. } else if (card.length() == 15) {
  87. String uyear = "19" + card.substring(6, 8);// 年份
  88. String uyue = card.substring(8, 10);// 月份
  89. String uri = card.substring(10, 12);// 得到日
  90. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  91. b = format.parse(uyear + "-" + uyue + "-" + uri);
  92. }
  93. return b;
  94. }
  95. }