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
665c8b18
Commit
665c8b18
authored
May 09, 2024
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
身份证识别
parent
acbd965d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
183 additions
and
2 deletions
+183
-2
DateHelper.java
...src/main/java/com/yanzuoguang/util/helper/DateHelper.java
+73
-0
TestDateHelper.java
yzg-util-base/src/test/java/helper/TestDateHelper.java
+110
-2
No files found.
yzg-util-base/src/main/java/com/yanzuoguang/util/helper/DateHelper.java
View file @
665c8b18
...
...
@@ -1169,6 +1169,79 @@ public class DateHelper {
return
sbTo
.
toString
();
}
/**
* 获取开始时间
*
* @param current 当前时间
* @param fieldValueFormat 结束时间呈现格式
* @return 返回值
*/
public
static
String
getFromTime
(
String
current
,
String
fieldValueFormat
)
{
return
getFromTime
(
DateHelper
.
getDateTime
(
current
),
fieldValueFormat
);
}
/**
* 获取开始时间
*
* @param current 当前时间
* @param fieldValueFormat 结束时间呈现格式
* @return 返回值
*/
public
static
String
getFromTime
(
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
(
"-01-01 00:00:00"
);
return
sbTo
.
toString
();
}
if
(
dateFormat
)
{
sbTo
.
append
(
"-"
).
append
(
DateHelper
.
getDateTimeString
(
"dd"
,
current
));
}
else
{
// 获取月份最后一天
sbTo
.
append
(
"-01 00:00:00"
);
return
sbTo
.
toString
();
}
if
(
hourFormat
)
{
sbTo
.
append
(
" "
).
append
(
DateHelper
.
getDateTimeString
(
"HH"
,
current
));
}
else
{
// 获取一天中最后1小时
sbTo
.
append
(
" 00:00:00"
);
return
sbTo
.
toString
();
}
if
(
minuteFormat
)
{
sbTo
.
append
(
":"
).
append
(
DateHelper
.
getDateTimeString
(
"mm"
,
current
));
}
else
{
// 获取一小时中最后一分钟
sbTo
.
append
(
":00:00"
);
return
sbTo
.
toString
();
}
if
(
secondFormat
)
{
sbTo
.
append
(
":"
).
append
(
DateHelper
.
getDateTimeString
(
"ss"
,
current
));
}
else
{
// 获取一分钟中最后一秒
sbTo
.
append
(
":00"
);
}
return
sbTo
.
toString
();
}
/**
* 每日日期拆分
*/
...
...
yzg-util-base/src/test/java/helper/TestDateHelper.java
View file @
665c8b18
...
...
@@ -160,11 +160,11 @@ public class TestDateHelper {
// 同一月中的最后小时
Assert
.
assertEquals
(
DateHelper
.
getToTime
(
"2023-12-12 15:03:59"
,
"yyyy-MM-0
0
HH:00:00"
),
DateHelper
.
getToTime
(
"2023-12-12 15:03:59"
,
"yyyy-MM-0
1
HH:00:00"
),
"2023-12-31 23:59:59"
);
Assert
.
assertEquals
(
DateHelper
.
getToTime
(
"2023-12-12 15:59:01"
,
"yyyy-MM-0
0
HH:00:00"
),
DateHelper
.
getToTime
(
"2023-12-12 15:59:01"
,
"yyyy-MM-0
1
HH:00:00"
),
"2023-12-31 23:59:59"
);
...
...
@@ -188,4 +188,112 @@ public class TestDateHelper {
"2023-12-31 23:59:59"
);
}
@Test
public
void
getFromDate
()
{
// 同一分钟的最后时间
Assert
.
assertEquals
(
DateHelper
.
getFromTime
(
"2023-12-12 15:59:59"
,
"yyyy-MM-dd HH:mm:ss"
),
"2023-12-12 15:59:59"
);
Assert
.
assertEquals
(
DateHelper
.
getFromTime
(
"2023-12-12 23:59:01"
,
"yyyy-MM-dd HH:mm:ss"
),
"2023-12-12 23:59:01"
);
// 同一分钟的最后时间
Assert
.
assertEquals
(
DateHelper
.
getFromTime
(
"2023-12-12 15:59:59"
,
"yyyy-MM-dd HH:mm:00"
),
"2023-12-12 15:59:00"
);
Assert
.
assertEquals
(
DateHelper
.
getFromTime
(
"2023-12-12 23:59:01"
,
"yyyy-MM-dd HH:mm:00"
),
"2023-12-12 23:59:00"
);
// 同一小时的最后时间
Assert
.
assertEquals
(
DateHelper
.
getFromTime
(
"2023-12-12 15:03:59"
,
"yyyy-MM-dd HH:00:00"
),
"2023-12-12 15:00:00"
);
Assert
.
assertEquals
(
DateHelper
.
getFromTime
(
"2023-12-12 15:59:01"
,
"yyyy-MM-dd HH:00:00"
),
"2023-12-12 15:00:00"
);
// 同一天的最后时间
Assert
.
assertEquals
(
DateHelper
.
getFromTime
(
"2023-12-12 12:03:59"
,
"yyyy-MM-dd 00:00:00"
),
"2023-12-12 00:00:00"
);
Assert
.
assertEquals
(
DateHelper
.
getFromTime
(
"2023-12-12 15:59:01"
,
"yyyy-MM-dd 00:00:00"
),
"2023-12-12 00:00:00"
);
// 同一月的最后时间
Assert
.
assertEquals
(
DateHelper
.
getFromTime
(
"2023-1-15 12:03:59"
,
"yyyy-MM-01 00:00:00"
),
"2023-01-01 00:00:00"
);
Assert
.
assertEquals
(
DateHelper
.
getFromTime
(
"2023-02-12 15:59:01"
,
"yyyy-MM-01 00:00:00"
),
"2023-02-01 00:00:00"
);
// 同一年的最后时间
Assert
.
assertEquals
(
DateHelper
.
getFromTime
(
"2023-12-12 15:59:59"
,
"yyyy-01-01 00:00:00"
),
"2023-01-01 00:00:00"
);
Assert
.
assertEquals
(
DateHelper
.
getFromTime
(
"2023-01-12 15:59:59"
,
"yyyy-01-01 00:00:00"
),
"2023-01-01 00:00:00"
);
Assert
.
assertEquals
(
DateHelper
.
getFromTime
(
"2023-01-12 15:59:59"
,
"yyyy-01-01 00:00:00"
),
"2023-01-01 00:00:00"
);
// 同一小时的最后秒,不计算分钟
Assert
.
assertEquals
(
DateHelper
.
getFromTime
(
"2023-12-12 15:03:59"
,
"yyyy-MM-dd HH:00:ss"
),
"2023-12-12 15:00:00"
);
Assert
.
assertEquals
(
DateHelper
.
getFromTime
(
"2023-12-12 15:59:01"
,
"yyyy-MM-dd HH:00:ss"
),
"2023-12-12 15:00:00"
);
// 同一月中的最后小时
Assert
.
assertEquals
(
DateHelper
.
getFromTime
(
"2023-12-12 15:03:59"
,
"yyyy-MM-01 HH:00:00"
),
"2023-12-01 00:00:00"
);
Assert
.
assertEquals
(
DateHelper
.
getFromTime
(
"2023-12-12 15:59:01"
,
"yyyy-MM-01 HH:00:00"
),
"2023-12-01 00:00:00"
);
// 同一年中的最后小时
Assert
.
assertEquals
(
DateHelper
.
getFromTime
(
"2023-05-12 15:03:59"
,
"yyyy-01-01 HH:00:00"
),
"2023-01-01 00:00:00"
);
Assert
.
assertEquals
(
DateHelper
.
getFromTime
(
"2023-12-12 15:59:01"
,
"yyyy-01-01 HH:00:00"
),
"2023-12-31 00:00:00"
);
// 同一年中的最后日
Assert
.
assertEquals
(
DateHelper
.
getFromTime
(
"2023-05-12 15:03:59"
,
"yyyy-01-dd 00:00:00"
),
"2023-01-01 00:00:00"
);
Assert
.
assertEquals
(
DateHelper
.
getFromTime
(
"2023-12-12 15:59:01"
,
"yyyy-01-dd 00:00:00"
),
"2023-01-01 00:00:00"
);
}
}
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