1.CodeUtil.java
package com.jiayou.peis.common.core.util;
import cn.hutool.core.util.IdcardUtil;
import com.github.houbb.pinyin.constant.enums.PinyinStyleEnum;
import com.github.houbb.pinyin.util.PinyinHelper;
import com.jiayou.peis.common.core.constant.ConstNumber;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
@Slf4j
@UtilityClass
public class CodeUtil {
final static DateTimeFormatter timeFormat = DateTimeFormatter.ofPattern("yyyyMMddHHmmSS");
final static DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyyMMdd");
final static SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
public String doPinyin(String character) {
return PinyinHelper.toPinyin(character, PinyinStyleEnum.FIRST_LETTER,"");
}
public String getOrderCode(int len) {
String orderCode = timeFormat.format(LocalDateTime.now());
return orderCode + String.valueOf(Math.random()).substring(2, 2 + len);
}
public String getDateCode(int len) {
String cardCode = timeFormat.format(LocalDateTime.now());
return cardCode + String.valueOf(Math.random()).substring(2, 2 + len);
}
public String getIdCardAge(String idCard){
return String.valueOf(IdcardUtil.getAgeByIdCard(idCard));
}
public String getIdCardMonth(String idCard){
String idCardMonth = getCardBirthDay(idCard);
SimpleDateFormat df=new SimpleDateFormat("MM");
String ageMonthStr = df.format(new Date());
return String.valueOf(Long.parseLong(ageMonthStr)-Long.parseLong(idCardMonth.substring(5,7)));
}
private static String getIdCardBirthDay(String idCard,boolean flag) {
String strStr= null;
try {
if (StringUtils.isEmpty(idCard) || idCard.length() != 15 && idCard.length() != 18) {
log.error("身份证件号有误,无法计算年龄");
return null;
}
if(flag){
Calendar cal = Calendar.getInstance();
int yearNow = cal.get(Calendar.YEAR);
int monthNow = cal.get(Calendar.MONTH)+1;
int dayNow = cal.get(Calendar.DATE);
long millNow = format.parse(yearNow+""+(monthNow < 10 ? 0+""+monthNow :monthNow)+""+(dayNow < 10 ? 0+""+dayNow:dayNow)).getTime();
long mill = format.parse(idCard.substring(6,14)).getTime();
long ag = (millNow - mill)/(1000*60*60)/(365*24);
strStr = String.valueOf(ag);
}else {
strStr = idCard.substring(6,14);
strStr = strStr.substring(0,4)+"-"+strStr.substring(4,6)+"-"+strStr.substring(6,8);
}
} catch (ParseException e) {
e.printStackTrace();
}
return strStr;
}
public String getCardBirthDay(String idCard){
return getIdCardBirthDay(idCard,false);
}
public String getIdCardGender(String idCard){
String gender = "";
if (!StringUtils.isEmpty(idCard)){
if(idCard.length()==18){
gender = Integer.parseInt(idCard.substring(16, 17)) % 2 == 0 ? "0" : "1";
}else if (idCard.length() == 15) {
gender = Integer.parseInt(idCard.substring(idCard.length() - 3, idCard.length())) % 2 == 0 ? "0" : "1";
}
}
return gender;
}
public String getRandomCode(int len) {
char[] chars = new char[len];
Random rnd = new Random();
for (int i = 0; i < len; i++) {
chars[i] = (char) ('0' + rnd.nextInt(10));
}
return new String(chars);
}
public String[] autoIdCardArray(String identityCard) {
if (StringUtils.isEmpty(identityCard)){
return null;
}
String[] strArr = new String[4];
String age = CodeUtil.getIdCardAge(identityCard) ;
String ageUnit = ConstNumber.STRING_ONE;
if (Integer.parseInt(age)==0){
age = CodeUtil.getIdCardMonth(identityCard);
ageUnit = ConstNumber.STRING_TWO;
}
strArr[0] = age;
strArr[1] = ageUnit;
strArr[2] = CodeUtil.getCardBirthDay(identityCard);
strArr[3] = CodeUtil.getIdCardGender(identityCard);
return strArr;
}
public String get18Ic(String identityCard) {
String retId = "";
String id17 = "";
int sum = 0;
int y = 0;
int[] wf = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
String[] cc = { "1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2" };
if (identityCard.length() != 15) {
return identityCard;
}
id17 = identityCard.substring(0, 6) + "19" + identityCard.substring(6);
for (int i = 0; i < 17; i++) {
sum = sum + Integer.valueOf(id17.substring(i, i + 1)) * wf[i];
}
y = sum % 11;
retId = id17 + cc[y];
return retId;
}
public static void main(String[] args) {
String[] strings = CodeUtil.autoIdCardArray("522130197406100765");
System.out.println(1);
System.out.println(1);
System.out.println(CodeUtil.doPinyin("组织与人力资源部(退休职工)"));
}
}
2.引入相应maven
<dependencies>
<!-- pinyin 是 java 实现的高性能中文拼音转换工具
参考说明:https://blog.csdn.net/ryo1060732496/article/details/104034630
-->
<dependency>
<groupId>com.github.houbb</groupId>
<artifactId>pinyin</artifactId>
<version>0.1.5</version>
</dependency>
</dependencies>