f42daa97cf9edcaf7dae5483c004fe14702ec370.svn-base 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package cn.com.goldenwater.util.common;
  2. import cn.com.goldenwater.dcproj.utils.StringUtils;
  3. import java.text.ParseException;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Date;
  6. import java.util.regex.Pattern;
  7. /**
  8. * IdCardUtils
  9. * 从身份证获取到出生年月 和 年龄
  10. *
  11. * @author lxf
  12. * @version 1.0
  13. * @date 2023/01/17 14:55
  14. **/
  15. public class IdCardUtils {
  16. /**
  17. * 15位身份证号
  18. * 1980年以前的身份证
  19. */
  20. private static final Integer FIFTEEN_ID_CARD=15;
  21. /**
  22. * 18位身份证号
  23. */
  24. private static final Integer EIGHTEEN_ID_CARD=18;
  25. /**
  26. * 出生日期格式化
  27. */
  28. private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  29. /**
  30. * 校验 15位到18位的身份证号
  31. */
  32. public static boolean checkIdCard(String idCard) {
  33. String regex = "^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$|^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X)$";
  34. return Pattern.matches(regex, idCard);
  35. }
  36. /**
  37. * 获取到出生日期 yyyy年mm月dd日
  38. * @param IDCard
  39. * @return
  40. */
  41. public static String getBirthday(String IDCard){
  42. String birthday="";
  43. String year="";
  44. String month="";
  45. String day="";
  46. if (StringUtils.isNotBlank(IDCard)){
  47. //15位身份证号
  48. if (IDCard.length() == FIFTEEN_ID_CARD){
  49. // 身份证上的年份(15位身份证为1980年前的)
  50. year = "19" + IDCard.substring(6, 8);
  51. //身份证上的月份
  52. month = IDCard.substring(8, 10);
  53. //身份证上的日期
  54. day= IDCard.substring(10, 12);
  55. //18位身份证号
  56. }else if(IDCard.length() == EIGHTEEN_ID_CARD){
  57. // 身份证上的年份
  58. year = IDCard.substring(6).substring(0, 4);
  59. // 身份证上的月份
  60. month = IDCard.substring(10).substring(0, 2);
  61. //身份证上的日期
  62. day=IDCard.substring(12).substring(0,2);
  63. }
  64. birthday=year+"年"+month+"月"+day+"日";
  65. }
  66. return birthday;
  67. }
  68. public static Date getBirthdayDate(String IDCard){
  69. String bornStr = getBirthday(IDCard);
  70. if(StringUtils.isBlank(bornStr)){
  71. return null;
  72. }
  73. SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
  74. try {
  75. Date bornDate = sdf.parse(bornStr);
  76. return bornDate;
  77. } catch (ParseException e) {
  78. e.printStackTrace();
  79. }
  80. return null;
  81. }
  82. /**
  83. * 获取年龄
  84. * @param IDCard
  85. * @return
  86. */
  87. public static Integer getAge(String IDCard){
  88. Integer age = 0;
  89. Date date = new Date();
  90. if (StringUtils.isNotBlank(IDCard)&& checkIdCard(IDCard)){
  91. // 当前年份
  92. String fyear = format.format(date).substring(0, 4);
  93. // 当前月份
  94. String fyue = format.format(date).substring(5, 7);
  95. //15位身份证号
  96. if (IDCard.length() == FIFTEEN_ID_CARD){
  97. // 身份证上的年份(15位身份证为1980年前的)
  98. String uyear = "19" + IDCard.substring(6, 8);
  99. // 身份证上的月份
  100. String uyue = IDCard.substring(8, 10);
  101. if (Integer.parseInt(uyue) <= Integer.parseInt(fyue)) {
  102. age = Integer.parseInt(fyear) - Integer.parseInt(uyear) + 1;
  103. // 当前用户还没过生
  104. } else {
  105. age = Integer.parseInt(fyear) - Integer.parseInt(uyear);
  106. }
  107. //18位身份证号
  108. }else if(IDCard.length() == EIGHTEEN_ID_CARD){
  109. // 身份证上的年份
  110. String year = IDCard.substring(6).substring(0, 4);
  111. // 身份证上的月份
  112. String yue = IDCard.substring(10).substring(0, 2);
  113. // 当前月份大于用户出身的月份表示已过生日
  114. if (Integer.parseInt(yue) <= Integer.parseInt(fyue)) {
  115. age = Integer.parseInt(fyear) - Integer.parseInt(year) + 1;
  116. // 当前用户还没过生日
  117. } else {
  118. age = Integer.parseInt(fyear) - Integer.parseInt(year);
  119. }
  120. }
  121. }
  122. return age;
  123. }
  124. /**
  125. * 获取到性别
  126. * @param IDCard
  127. * @return
  128. */
  129. public static String getSex(String IDCard){
  130. String sex ="";
  131. if (StringUtils.isNotBlank(IDCard)){
  132. //15位身份证号
  133. if (IDCard.length() == FIFTEEN_ID_CARD){
  134. if (Integer.parseInt(IDCard.substring(14, 15)) % 2 == 0) {
  135. sex = "女";
  136. } else {
  137. sex = "男";
  138. }
  139. //18位身份证号
  140. }else if(IDCard.length() == EIGHTEEN_ID_CARD){
  141. // 判断性别
  142. if (Integer.parseInt(IDCard.substring(16).substring(0, 1)) % 2 == 0) {
  143. sex = "女";
  144. } else {
  145. sex = "男";
  146. }
  147. }
  148. }
  149. return sex;
  150. }
  151. }