Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
Y
yzg-util
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
YZG
yzg-util
Commits
acbd965d
Commit
acbd965d
authored
Apr 28, 2024
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
身份证识别
parent
cb7bc202
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
187 additions
and
4 deletions
+187
-4
DateHelper.java
...src/main/java/com/yanzuoguang/util/helper/DateHelper.java
+76
-0
TestDateHelper.java
yzg-util-base/src/test/java/helper/TestDateHelper.java
+111
-4
No files found.
yzg-util-base/src/main/java/com/yanzuoguang/util/helper/DateHelper.java
View file @
acbd965d
...
@@ -1093,6 +1093,82 @@ public class DateHelper {
...
@@ -1093,6 +1093,82 @@ public class DateHelper {
return
ret
;
return
ret
;
}
}
/**
* 获取结束时间
*
* @param current 当前时间
* @param fieldValueFormat 结束时间呈现格式
* @return 返回值
*/
public
static
String
getToTime
(
String
current
,
String
fieldValueFormat
)
{
return
getToTime
(
DateHelper
.
getDateTime
(
current
),
fieldValueFormat
);
}
/**
* 获取结束时间
*
* @param current 当前时间
* @param fieldValueFormat 结束时间呈现格式
* @return 返回值
*/
public
static
String
getToTime
(
Date
current
,
String
fieldValueFormat
)
{
boolean
yearFormat
=
fieldValueFormat
.
contains
(
"yyyy"
);
boolean
monthFormat
=
fieldValueFormat
.
contains
(
"MM"
);
boolean
dateFormat
=
fieldValueFormat
.
contains
(
"dd"
);
boolean
hourFormat
=
fieldValueFormat
.
contains
(
"HH"
);
boolean
minuteFormat
=
fieldValueFormat
.
contains
(
"mm"
);
boolean
secondFormat
=
fieldValueFormat
.
contains
(
"ss"
);
StringBuilder
sbTo
=
new
StringBuilder
();
if
(
yearFormat
)
{
sbTo
.
append
(
DateHelper
.
getDateTimeString
(
"yyyy"
,
current
));
}
else
{
return
StringHelper
.
EMPTY
;
}
if
(
monthFormat
)
{
sbTo
.
append
(
"-"
).
append
(
DateHelper
.
getDateTimeString
(
"MM"
,
current
));
}
else
{
// 获取年份最后一月
sbTo
.
append
(
"-12-31 23:59:59"
);
return
sbTo
.
toString
();
}
if
(
dateFormat
)
{
sbTo
.
append
(
"-"
).
append
(
DateHelper
.
getDateTimeString
(
"dd"
,
current
));
}
else
{
// 获取月份最后一天
Date
monthEnd
=
DateHelper
.
getMonthEnd
(
DateHelper
.
getDateTime
(
sbTo
+
"-01"
));
sbTo
.
append
(
"-"
)
.
append
(
DateHelper
.
getDateTimeString
(
"dd"
,
monthEnd
))
.
append
(
" 23:59:59"
);
return
sbTo
.
toString
();
}
if
(
hourFormat
)
{
sbTo
.
append
(
" "
).
append
(
DateHelper
.
getDateTimeString
(
"HH"
,
current
));
}
else
{
// 获取一天中最后1小时
sbTo
.
append
(
" 23:59:59"
);
return
sbTo
.
toString
();
}
if
(
minuteFormat
)
{
sbTo
.
append
(
":"
).
append
(
DateHelper
.
getDateTimeString
(
"mm"
,
current
));
}
else
{
// 获取一小时中最后一分钟
sbTo
.
append
(
":59:59"
);
return
sbTo
.
toString
();
}
if
(
secondFormat
)
{
sbTo
.
append
(
":"
).
append
(
DateHelper
.
getDateTimeString
(
"ss"
,
current
));
}
else
{
// 获取一分钟中最后一秒
sbTo
.
append
(
":59"
);
}
return
sbTo
.
toString
();
}
/**
/**
* 每日日期拆分
* 每日日期拆分
*/
*/
...
...
yzg-util-base/src/test/java/helper/TestDateHelper.java
View file @
acbd965d
...
@@ -76,9 +76,116 @@ public class TestDateHelper {
...
@@ -76,9 +76,116 @@ public class TestDateHelper {
@Test
@Test
public
void
testDayMonth
()
{
public
void
testDayMonth
()
{
Date
dt
=
DateHelper
.
getDateTime
(
"2023-12-12 15:59:59"
);
Date
dt
=
DateHelper
.
getDateTime
(
"2023-12-12 15:59:59"
);
Assert
.
assertEquals
(
2023
,
DateHelper
.
getYear
(
dt
));
Assert
.
assertEquals
(
2023
,
DateHelper
.
getYear
(
dt
));
Assert
.
assertEquals
(
12
,
DateHelper
.
getMonth
(
dt
));
Assert
.
assertEquals
(
12
,
DateHelper
.
getMonth
(
dt
));
Assert
.
assertEquals
(
12
,
DateHelper
.
getDay
(
dt
));
Assert
.
assertEquals
(
12
,
DateHelper
.
getDay
(
dt
));
Assert
.
assertEquals
(
15
,
DateHelper
.
getHour
(
dt
));
Assert
.
assertEquals
(
15
,
DateHelper
.
getHour
(
dt
));
}
@Test
public
void
getEndDate
()
{
// 同一分钟的最后时间
Assert
.
assertEquals
(
DateHelper
.
getToTime
(
"2023-12-12 15:59:59"
,
"yyyy-MM-dd HH:mm:ss"
),
"2023-12-12 15:59:59"
);
Assert
.
assertEquals
(
DateHelper
.
getToTime
(
"2023-12-12 23:59:01"
,
"yyyy-MM-dd HH:mm:ss"
),
"2023-12-12 23:59:01"
);
// 同一分钟的最后时间
Assert
.
assertEquals
(
DateHelper
.
getToTime
(
"2023-12-12 15:59:59"
,
"yyyy-MM-dd HH:mm:00"
),
"2023-12-12 15:59:59"
);
Assert
.
assertEquals
(
DateHelper
.
getToTime
(
"2023-12-12 23:59:01"
,
"yyyy-MM-dd HH:mm:00"
),
"2023-12-12 23:59:59"
);
// 同一小时的最后时间
Assert
.
assertEquals
(
DateHelper
.
getToTime
(
"2023-12-12 15:03:59"
,
"yyyy-MM-dd HH:00:00"
),
"2023-12-12 15:59:59"
);
Assert
.
assertEquals
(
DateHelper
.
getToTime
(
"2023-12-12 15:59:01"
,
"yyyy-MM-dd HH:00:00"
),
"2023-12-12 15:59:59"
);
// 同一天的最后时间
Assert
.
assertEquals
(
DateHelper
.
getToTime
(
"2023-12-12 12:03:59"
,
"yyyy-MM-dd 00:00:00"
),
"2023-12-12 23:59:59"
);
Assert
.
assertEquals
(
DateHelper
.
getToTime
(
"2023-12-12 15:59:01"
,
"yyyy-MM-dd 00:00:00"
),
"2023-12-12 23:59:59"
);
// 同一月的最后时间
Assert
.
assertEquals
(
DateHelper
.
getToTime
(
"2023-1-15 12:03:59"
,
"yyyy-MM-01 00:00:00"
),
"2023-01-31 23:59:59"
);
Assert
.
assertEquals
(
DateHelper
.
getToTime
(
"2023-02-12 15:59:01"
,
"yyyy-MM-01 00:00:00"
),
"2023-02-28 23:59:59"
);
// 同一年的最后时间
Assert
.
assertEquals
(
DateHelper
.
getToTime
(
"2023-12-12 15:59:59"
,
"yyyy-01-01 00:00:00"
),
"2023-12-31 23:59:59"
);
Assert
.
assertEquals
(
DateHelper
.
getToTime
(
"2023-01-12 15:59:59"
,
"yyyy-01-01 00:00:00"
),
"2023-12-31 23:59:59"
);
Assert
.
assertEquals
(
DateHelper
.
getToTime
(
"2023-01-12 15:59:59"
,
"yyyy-01-01 00:00:00"
),
"2023-12-31 23:59:59"
);
// 同一小时的最后秒,不计算分钟
Assert
.
assertEquals
(
DateHelper
.
getToTime
(
"2023-12-12 15:03:59"
,
"yyyy-MM-dd HH:00:ss"
),
"2023-12-12 15:59:59"
);
Assert
.
assertEquals
(
DateHelper
.
getToTime
(
"2023-12-12 15:59:01"
,
"yyyy-MM-dd HH:00:ss"
),
"2023-12-12 15:59:59"
);
// 同一月中的最后小时
Assert
.
assertEquals
(
DateHelper
.
getToTime
(
"2023-12-12 15:03:59"
,
"yyyy-MM-00 HH:00:00"
),
"2023-12-31 23:59:59"
);
Assert
.
assertEquals
(
DateHelper
.
getToTime
(
"2023-12-12 15:59:01"
,
"yyyy-MM-00 HH:00:00"
),
"2023-12-31 23:59:59"
);
// 同一年中的最后小时
Assert
.
assertEquals
(
DateHelper
.
getToTime
(
"2023-05-12 15:03:59"
,
"yyyy-01-01 HH:00:00"
),
"2023-12-31 23:59:59"
);
Assert
.
assertEquals
(
DateHelper
.
getToTime
(
"2023-12-12 15:59:01"
,
"yyyy-01-01 HH:00:00"
),
"2023-12-31 23:59:59"
);
// 同一年中的最后日
Assert
.
assertEquals
(
DateHelper
.
getToTime
(
"2023-05-12 15:03:59"
,
"yyyy-01-dd 00:00:00"
),
"2023-12-31 23:59:59"
);
Assert
.
assertEquals
(
DateHelper
.
getToTime
(
"2023-12-12 15:59:01"
,
"yyyy-01-dd 00:00:00"
),
"2023-12-31 23:59:59"
);
}
}
}
}
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