|
|
@ -4,6 +4,7 @@ import lombok.extern.slf4j.Slf4j; |
|
|
|
|
|
|
|
|
|
|
|
import java.sql.Timestamp; |
|
|
|
import java.sql.Timestamp; |
|
|
|
import java.time.LocalDate; |
|
|
|
import java.time.LocalDate; |
|
|
|
|
|
|
|
import java.time.LocalDateTime; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 身份证工具类 |
|
|
|
* 身份证工具类 |
|
|
@ -19,14 +20,20 @@ public class IdCardUtils { |
|
|
|
private Integer gender; // 性别(1-男,2-女)
|
|
|
|
private Integer gender; // 性别(1-男,2-女)
|
|
|
|
private Integer age; // 年龄(0:未满一年的婴儿)
|
|
|
|
private Integer age; // 年龄(0:未满一年的婴儿)
|
|
|
|
private Timestamp birthday; // 出生日期
|
|
|
|
private Timestamp birthday; // 出生日期
|
|
|
|
|
|
|
|
private boolean onYearBthOver; // 同年生日是否已过
|
|
|
|
private boolean babyFlag; // 是否婴儿标识(T-是,F-否)
|
|
|
|
private boolean babyFlag; // 是否婴儿标识(T-是,F-否)
|
|
|
|
|
|
|
|
|
|
|
|
// 以下默认值为-1,如果 !=-1 则表示已经计算过值了,不需要重新计算浪费CPU资源
|
|
|
|
// 以下默认值为-1,如果 !=-1 则表示已经计算过值了,不需要重新计算
|
|
|
|
private int babyBirthMth = -1; // 宝宝出生余几个月(主要针对新生儿?如:0岁5个月 或 1岁3个月)
|
|
|
|
private int birthMth = -1; // 出生余几个月(如:0岁5个月 或 1岁3个月)
|
|
|
|
private int babyBirthDayOfMth = -1; // 宝宝出生余几天(主要针对新生儿?如:0岁2月零3天)
|
|
|
|
private int birthDayOfMth = -1; // 出生余几天(如:0岁2月零3天)
|
|
|
|
private int babyBirthWeek = -1; // 宝宝出生余几周(主要针对新生儿?如:0岁3周)
|
|
|
|
private int birthWeek = -1; // 出生余几周(如:0岁3周)
|
|
|
|
private int babyBirthDayOfWeek = -1; // 宝宝出生余N天(主要针对新生儿如?:3周零2天)
|
|
|
|
private int birthDayOfWeek = -1; // 出生余N天(如:3周零2天)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 性别(1-男,2-女) |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @return |
|
|
|
|
|
|
|
*/ |
|
|
|
public int getGender() { |
|
|
|
public int getGender() { |
|
|
|
return gender; |
|
|
|
return gender; |
|
|
|
} |
|
|
|
} |
|
|
@ -71,7 +78,12 @@ public class IdCardUtils { |
|
|
|
return birthday == null ? null : DateUtils.toString(birthday, format); |
|
|
|
return birthday == null ? null : DateUtils.toString(birthday, format); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public boolean isBabyFlag() { |
|
|
|
/** |
|
|
|
|
|
|
|
* 是否婴儿(true: 未满一周岁) |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @return |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public boolean isBaby() { |
|
|
|
return babyFlag; |
|
|
|
return babyFlag; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -80,18 +92,19 @@ public class IdCardUtils { |
|
|
|
* |
|
|
|
* |
|
|
|
* @return N月 |
|
|
|
* @return N月 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public int getBabyBirthMth() { |
|
|
|
public int getBirthMth() { |
|
|
|
if (babyBirthMth != -1) { |
|
|
|
if (birthMth != -1) { |
|
|
|
return babyBirthMth; |
|
|
|
return birthMth; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
LocalDate bth = birthday.toLocalDateTime().toLocalDate(); |
|
|
|
LocalDate bth = birthday.toLocalDateTime().toLocalDate(); |
|
|
|
LocalDate ld = LocalDate.now(); |
|
|
|
LocalDate ld = LocalDate.now(); |
|
|
|
babyBirthMth = ld.getMonthValue() - bth.getMonthValue(); |
|
|
|
|
|
|
|
if (babyBirthMth > 0 && ld.getDayOfMonth() < bth.getDayOfMonth()) { |
|
|
|
birthMth = ld.getMonthValue() - bth.getMonthValue(); |
|
|
|
babyBirthMth--; |
|
|
|
if (birthMth < 0) { |
|
|
|
|
|
|
|
birthMth = 12 + birthMth; |
|
|
|
} |
|
|
|
} |
|
|
|
return babyBirthMth; |
|
|
|
return birthMth; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -99,12 +112,11 @@ public class IdCardUtils { |
|
|
|
* |
|
|
|
* |
|
|
|
* @return N周 |
|
|
|
* @return N周 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public int getBabyBirthWeekOfYear() { |
|
|
|
public int getBirthWeekOfYear() { |
|
|
|
if (babyBirthWeek != -1) { |
|
|
|
if (birthWeek < 0) { |
|
|
|
return babyBirthWeek; |
|
|
|
birthWeek = (int) ((DateUtils.toMillis(LocalDate.now()) - DateUtils.toMillis(birthday.toLocalDateTime().plusYears(age).toLocalDate())) / DateUtils.getWeekMillis()); |
|
|
|
} |
|
|
|
} |
|
|
|
babyBirthWeek = (int) ((DateUtils.toMillis(LocalDate.now()) - DateUtils.toMillis(birthday.toLocalDateTime().plusYears(age).toLocalDate())) / DateUtils.getWeekMillis()); |
|
|
|
return birthWeek; |
|
|
|
return babyBirthWeek; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -112,8 +124,8 @@ public class IdCardUtils { |
|
|
|
* |
|
|
|
* |
|
|
|
* @return N周 |
|
|
|
* @return N周 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public int getBabyBirthWeekOfMth() { |
|
|
|
public int getBirthWeekOfMth() { |
|
|
|
return (int) ((DateUtils.toMillis(LocalDate.now()) - DateUtils.toMillis(birthday.toLocalDateTime().plusYears(age).plusMonths(getBabyBirthMth()).toLocalDate())) / DateUtils.getWeekMillis()); |
|
|
|
return (int) ((DateUtils.toMillis(LocalDate.now()) - DateUtils.toMillis(birthday.toLocalDateTime().plusYears(age).plusMonths(getBirthMth()).toLocalDate())) / DateUtils.getWeekMillis()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -121,17 +133,24 @@ public class IdCardUtils { |
|
|
|
* |
|
|
|
* |
|
|
|
* @return N天 |
|
|
|
* @return N天 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public int getBabyBirthDayOfMonth() { |
|
|
|
public int getBirthDayOfMonth() { |
|
|
|
if (babyBirthDayOfMth != -1) { |
|
|
|
if (birthDayOfMth != -1) { |
|
|
|
return babyBirthDayOfMth; |
|
|
|
return birthDayOfMth; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
LocalDate ld = LocalDate.now(); |
|
|
|
LocalDate ld = LocalDate.now(); |
|
|
|
LocalDate mth = birthday.toLocalDateTime().plusYears(age).plusMonths(getBabyBirthMth()).toLocalDate(); |
|
|
|
if (onYearBthOver) { |
|
|
|
int i = 1; |
|
|
|
LocalDateTime dateTime = birthday.toLocalDateTime(); |
|
|
|
for (; mth.plusDays(i).isBefore(ld); i++) ; |
|
|
|
if (ld.getMonthValue() == dateTime.getMonthValue()) { |
|
|
|
|
|
|
|
birthDayOfMth = ld.getDayOfMonth() - dateTime.getDayOfMonth(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (birthDayOfMth < 0) { |
|
|
|
|
|
|
|
birthDayOfMth = ld.getDayOfMonth(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return babyBirthDayOfMth = i; |
|
|
|
return birthDayOfMth; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -139,12 +158,12 @@ public class IdCardUtils { |
|
|
|
* |
|
|
|
* |
|
|
|
* @return N天 |
|
|
|
* @return N天 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public int getBabyBirthDayOfWeek() { |
|
|
|
public int getBirthDayOfWeek() { |
|
|
|
if (babyBirthDayOfWeek != -1) return babyBirthDayOfWeek; |
|
|
|
if (birthDayOfWeek != -1) return birthDayOfWeek; |
|
|
|
long bthMills = DateUtils.toMillis(birthday.toLocalDateTime().plusYears(age).plusMonths(getBabyBirthMth()).plusWeeks(getBabyBirthWeekOfMth())); |
|
|
|
long bthMills = DateUtils.toMillis(birthday.toLocalDateTime().plusYears(age).plusMonths(getBirthMth()).plusWeeks(getBirthWeekOfMth())); |
|
|
|
babyBirthDayOfWeek = (int) ((DateUtils.toMillis(LocalDate.now()) - bthMills) / DateUtils.getDayMillis()); |
|
|
|
birthDayOfWeek = (int) ((DateUtils.toMillis(LocalDate.now()) - bthMills) / DateUtils.getDayMillis()); |
|
|
|
if (babyBirthDayOfWeek > 0) babyBirthDayOfWeek++; |
|
|
|
if (birthDayOfWeek > 0) birthDayOfWeek++; |
|
|
|
return babyBirthDayOfWeek; |
|
|
|
return birthDayOfWeek; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -178,23 +197,22 @@ public class IdCardUtils { |
|
|
|
idCard.gender = Integer.valueOf(chars[genderIdx] + "") % 2 == 0 ? 2 : 1; |
|
|
|
idCard.gender = Integer.valueOf(chars[genderIdx] + "") % 2 == 0 ? 2 : 1; |
|
|
|
|
|
|
|
|
|
|
|
LocalDate today = DateUtils.now().toLocalDateTime().toLocalDate(); |
|
|
|
LocalDate today = DateUtils.now().toLocalDateTime().toLocalDate(); |
|
|
|
int todayYear = today.getYear(); |
|
|
|
int onYear = today.getYear(); |
|
|
|
idCard.age = todayYear - bth.getYear(); |
|
|
|
idCard.age = onYear - bth.getYear(); |
|
|
|
if (idCard.age > 0) { |
|
|
|
if (idCard.age > 0) { |
|
|
|
LocalDate dateTime = bth.withYear(todayYear); |
|
|
|
if (idCard.onYearBthOver = today.compareTo(bth.withYear(onYear)) < 0) { |
|
|
|
if (today.compareTo(dateTime) < 0) { |
|
|
|
|
|
|
|
idCard.age--; // 生日未到,年龄减一
|
|
|
|
idCard.age--; // 生日未到,年龄减一
|
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
idCard.babyFlag = true; |
|
|
|
idCard.babyFlag = true; |
|
|
|
idCard.babyBirthMth = today.getMonthValue() - bth.getMonthValue(); |
|
|
|
idCard.birthMth = today.getMonthValue() - bth.getMonthValue(); |
|
|
|
if (idCard.babyBirthMth > 0 && today.getDayOfMonth() > bth.getDayOfMonth()) { |
|
|
|
if (idCard.birthMth > 0 && today.getDayOfMonth() > bth.getDayOfMonth()) { |
|
|
|
idCard.babyBirthMth--; |
|
|
|
idCard.birthMth--; |
|
|
|
} |
|
|
|
} |
|
|
|
LocalDate mth = bth.withMonth(idCard.babyBirthMth); |
|
|
|
LocalDate mth = bth.withMonth(idCard.birthMth); |
|
|
|
int i = 1; |
|
|
|
int i = 1; |
|
|
|
for (; mth.plusDays(i).isBefore(today); i++) ; |
|
|
|
for (; mth.plusDays(i).isBefore(today); i++) ; |
|
|
|
idCard.babyBirthDayOfMth = i; |
|
|
|
idCard.birthDayOfMth = i; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return idCard; |
|
|
|
return idCard; |
|
|
|