Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
邬吉朋
ops
Commits
f1a74efb
Commit
f1a74efb
authored
Feb 25, 2025
by
zhuxiang
Browse files
身份证中提取用户出生日期、生日、年龄
parent
452c13ee
Changes
3
Show whitespace changes
Inline
Side-by-side
src/main/java/com/ruzhoukj/sms/entity/UserEntity.java
View file @
f1a74efb
...
@@ -42,7 +42,7 @@ public class UserEntity {
...
@@ -42,7 +42,7 @@ public class UserEntity {
private
Integer
sex
;
private
Integer
sex
;
@TableField
(
"birth_date"
)
@TableField
(
"birth_date"
)
private
Date
birthDate
;
private
String
birthDate
;
@TableField
(
"birth"
)
@TableField
(
"birth"
)
private
String
birth
;
private
String
birth
;
...
...
src/main/java/com/ruzhoukj/sms/service/impl/UserServiceImpl.java
View file @
f1a74efb
...
@@ -3,6 +3,7 @@ package com.ruzhoukj.sms.service.impl;
...
@@ -3,6 +3,7 @@ package com.ruzhoukj.sms.service.impl;
import
com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
;
import
com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
;
import
com.platform.exception.BusinessException
;
import
com.platform.exception.BusinessException
;
import
com.platform.utils.DozerUtils
;
import
com.platform.utils.DozerUtils
;
import
com.platform.utils.StringUtils
;
import
com.ruzhoukj.sms.dao.UserDao
;
import
com.ruzhoukj.sms.dao.UserDao
;
import
com.ruzhoukj.sms.entity.ProjectEntity
;
import
com.ruzhoukj.sms.entity.ProjectEntity
;
import
com.ruzhoukj.sms.entity.UserEntity
;
import
com.ruzhoukj.sms.entity.UserEntity
;
...
@@ -11,10 +12,13 @@ import com.ruzhoukj.sms.model.dto.UserDTO;
...
@@ -11,10 +12,13 @@ import com.ruzhoukj.sms.model.dto.UserDTO;
import
com.ruzhoukj.sms.model.vo.UserVO
;
import
com.ruzhoukj.sms.model.vo.UserVO
;
import
com.ruzhoukj.sms.service.UserExtendService
;
import
com.ruzhoukj.sms.service.UserExtendService
;
import
com.ruzhoukj.sms.service.UserService
;
import
com.ruzhoukj.sms.service.UserService
;
import
com.ruzhoukj.sms.util.IDCardUtils
;
import
org.springframework.stereotype.Service
;
import
org.springframework.stereotype.Service
;
import
org.springframework.transaction.annotation.Transactional
;
import
org.springframework.transaction.annotation.Transactional
;
import
javax.annotation.Resource
;
import
javax.annotation.Resource
;
import
java.time.LocalDate
;
import
java.time.format.DateTimeFormatter
;
import
java.util.List
;
import
java.util.List
;
/**
/**
...
@@ -46,6 +50,18 @@ public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements
...
@@ -46,6 +50,18 @@ public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements
@Override
@Override
@Transactional
(
rollbackFor
=
Exception
.
class
)
@Transactional
(
rollbackFor
=
Exception
.
class
)
public
void
saveUser
(
UserDTO
user
)
{
public
void
saveUser
(
UserDTO
user
)
{
//处理生日、出生日期、年龄
if
(
StringUtils
.
isNotEmpty
(
user
.
getIdCard
()))
{
LocalDate
birthDate
=
IDCardUtils
.
getBirthDate
(
user
.
getIdCard
());
String
birth
=
IDCardUtils
.
getBirth
(
birthDate
);
int
age
=
IDCardUtils
.
calculateAge
(
birthDate
);
user
.
setAge
(
age
);
user
.
setBirth
(
birth
);
DateTimeFormatter
formatter1
=
DateTimeFormatter
.
ofPattern
(
"yyyy-MM-dd"
);
String
formattedDate1
=
birthDate
.
format
(
formatter1
);
user
.
setBirthDate
(
formattedDate1
);
}
if
(
save
(
user
))
{
if
(
save
(
user
))
{
UserExtend
userExtend
=
user
.
getUserExtend
();
UserExtend
userExtend
=
user
.
getUserExtend
();
userExtend
.
setUserId
(
user
.
getId
());
userExtend
.
setUserId
(
user
.
getId
());
...
...
src/main/java/com/ruzhoukj/sms/util/IDCardUtils.java
0 → 100644
View file @
f1a74efb
package
com.ruzhoukj.sms.util
;
import
java.time.LocalDate
;
import
java.time.Period
;
import
java.time.format.DateTimeFormatter
;
import
java.time.format.DateTimeParseException
;
public
class
IDCardUtils
{
/**
* 从身份证号中提取出生日期
*
* @param idCard 身份证号
* @return 出生日期(LocalDate)
*/
public
static
LocalDate
getBirthDate
(
String
idCard
)
{
if
(
idCard
==
null
||
idCard
.
length
()
!=
18
)
{
throw
new
IllegalArgumentException
(
"身份证号格式不正确"
);
}
String
birthDateStr
=
idCard
.
substring
(
6
,
14
);
// 提取出生日期部分
try
{
return
LocalDate
.
parse
(
birthDateStr
,
DateTimeFormatter
.
ofPattern
(
"yyyyMMdd"
));
}
catch
(
DateTimeParseException
e
)
{
throw
new
IllegalArgumentException
(
"身份证号中的出生日期格式不正确"
);
}
}
/**
* 从身份证号中提取生日
*
* @param birthDate 出生日期
* @return 生日
*/
public
static
String
getBirth
(
LocalDate
birthDate
)
{
return
birthDate
.
getMonthValue
()
+
"-"
+
birthDate
.
getDayOfMonth
();
}
/**
* 计算年龄
*
* @param birthDate 出生日期(LocalDate)
* @return 年龄
*/
public
static
int
calculateAge
(
LocalDate
birthDate
)
{
if
(
birthDate
==
null
)
{
throw
new
IllegalArgumentException
(
"出生日期不能为空"
);
}
LocalDate
currentDate
=
LocalDate
.
now
();
return
Period
.
between
(
birthDate
,
currentDate
).
getYears
();
}
public
static
void
main
(
String
[]
args
)
{
String
idCard
=
"320311198705281234"
;
// 示例身份证号
try
{
LocalDate
birthDate
=
getBirthDate
(
idCard
);
// 提取出生日期
System
.
out
.
println
(
"出生日期: "
+
birthDate
);
String
birth
=
getBirth
(
birthDate
);
// 提取出生日期
System
.
out
.
println
(
"生日: "
+
birth
);
DateTimeFormatter
formatter1
=
DateTimeFormatter
.
ofPattern
(
"yyyy-MM-dd"
);
String
formattedDate1
=
birthDate
.
format
(
formatter1
);
System
.
out
.
println
(
"格式化后的日期 (yyyy-MM-dd): "
+
formattedDate1
);
int
age
=
calculateAge
(
birthDate
);
// 计算年龄
System
.
out
.
println
(
"年龄: "
+
age
);
}
catch
(
IllegalArgumentException
e
)
{
System
.
err
.
println
(
"错误: "
+
e
.
getMessage
());
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment