Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yd-cloud-core
Overview
Overview
Details
Activity
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
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
xingmin
yd-cloud-core
Commits
2cefbb03
Commit
2cefbb03
authored
Oct 15, 2025
by
zhangxingmin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
配置
parent
a73bc14f
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
140 additions
and
28 deletions
+140
-28
yd-common/pom.xml
+5
-0
yd-common/src/main/java/com/yd/common/utils/ChineseTextConverter.java
+60
-0
yd-common/src/main/java/com/yd/common/utils/DateUtil.java
+75
-28
No files found.
yd-common/pom.xml
View file @
2cefbb03
...
...
@@ -59,5 +59,10 @@
<groupId>
org.apache.tomcat.embed
</groupId>
<artifactId>
tomcat-embed-core
</artifactId>
</dependency>
<dependency>
<groupId>
com.ibm.icu
</groupId>
<artifactId>
icu4j
</artifactId>
</dependency>
</dependencies>
</project>
yd-common/src/main/java/com/yd/common/utils/ChineseTextConverter.java
0 → 100644
View file @
2cefbb03
package
com
.
yd
.
common
.
utils
;
import
com.ibm.icu.text.Transliterator
;
/**
* 繁体字转简体中文工具类
* 使用 ICU4J 实现
*/
public
class
ChineseTextConverter
{
private
static
final
Transliterator
TRADITIONAL_TO_SIMPLIFIED
;
private
static
final
Transliterator
SIMPLIFIED_TO_TRADITIONAL
;
static
{
// 初始化转换器
TRADITIONAL_TO_SIMPLIFIED
=
Transliterator
.
getInstance
(
"Traditional-Simplified"
);
SIMPLIFIED_TO_TRADITIONAL
=
Transliterator
.
getInstance
(
"Simplified-Traditional"
);
}
/**
* 繁体转简体
* @param traditionalText
* @return
*/
public
static
String
traditionalToSimplified
(
String
traditionalText
)
{
if
(
traditionalText
==
null
||
traditionalText
.
trim
().
isEmpty
())
{
return
traditionalText
;
}
try
{
return
TRADITIONAL_TO_SIMPLIFIED
.
transliterate
(
traditionalText
);
}
catch
(
Exception
e
)
{
System
.
err
.
println
(
"转换失败: "
+
e
.
getMessage
());
return
traditionalText
;
}
}
/**
* 简体转繁体
* @param simplifiedText
* @return
*/
public
static
String
simplifiedToTraditional
(
String
simplifiedText
)
{
if
(
simplifiedText
==
null
||
simplifiedText
.
trim
().
isEmpty
())
{
return
simplifiedText
;
}
try
{
return
SIMPLIFIED_TO_TRADITIONAL
.
transliterate
(
simplifiedText
);
}
catch
(
Exception
e
)
{
System
.
err
.
println
(
"转换失败: "
+
e
.
getMessage
());
return
simplifiedText
;
}
}
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
traditionalToSimplified
(
"大學或以上"
));
}
}
\ No newline at end of file
yd-common/src/main/java/com/yd/common/utils/DateUtil.java
View file @
2cefbb03
...
...
@@ -4,6 +4,7 @@ import org.apache.commons.lang3.StringUtils;
import
java.text.ParseException
;
import
java.text.SimpleDateFormat
;
import
java.time.LocalDate
;
import
java.time.LocalDateTime
;
import
java.time.ZoneId
;
import
java.time.format.DateTimeFormatter
;
...
...
@@ -13,18 +14,22 @@ import java.util.Objects;
public
class
DateUtil
{
public
static
String
formatDate
(
Object
date
)
{
if
(
date
==
null
)
return
""
;
if
(
date
instanceof
Date
)
{
return
new
SimpleDateFormat
(
"yyyy/MM/dd"
).
format
((
Date
)
date
);
try
{
if
(
date
==
null
)
return
""
;
if
(
date
instanceof
Date
)
{
return
new
SimpleDateFormat
(
"yyyy/MM/dd"
).
format
((
Date
)
date
);
}
return
date
.
toString
();
}
catch
(
Exception
e
)
{
return
""
;
}
return
date
.
toString
();
}
public
static
Date
parseDate
(
String
dateStr
)
{
try
{
return
new
SimpleDateFormat
(
"yyyy/MM/dd"
).
parse
(
dateStr
);
}
catch
(
Parse
Exception
e
)
{
return
n
ew
Date
()
;
}
catch
(
Exception
e
)
{
return
n
ull
;
}
}
...
...
@@ -34,12 +39,16 @@ public class DateUtil {
* @return
*/
public
static
String
getyyyyMMdd
(
LocalDateTime
localDateTime
)
{
if
(
Objects
.
isNull
(
localDateTime
))
{
try
{
if
(
Objects
.
isNull
(
localDateTime
))
{
return
""
;
}
DateTimeFormatter
formatter
=
DateTimeFormatter
.
ofPattern
(
"yyyy/MM/dd"
);
return
localDateTime
.
format
(
formatter
);
}
catch
(
Exception
e
)
{
return
""
;
}
DateTimeFormatter
formatter
=
DateTimeFormatter
.
ofPattern
(
"yyyy/MM/dd"
);
return
localDateTime
.
format
(
formatter
);
}
/**
...
...
@@ -48,12 +57,16 @@ public class DateUtil {
* @return
*/
public
static
String
getHHmm
(
LocalDateTime
localDateTime
)
{
if
(
Objects
.
isNull
(
localDateTime
))
{
try
{
if
(
Objects
.
isNull
(
localDateTime
))
{
return
""
;
}
DateTimeFormatter
formatter
=
DateTimeFormatter
.
ofPattern
(
"HH:mm"
);
return
localDateTime
.
format
(
formatter
);
}
catch
(
Exception
e
)
{
return
""
;
}
DateTimeFormatter
formatter
=
DateTimeFormatter
.
ofPattern
(
"HH:mm"
);
return
localDateTime
.
format
(
formatter
);
}
/**
...
...
@@ -62,12 +75,16 @@ public class DateUtil {
* @return
*/
public
static
String
getyyyyMMddHHmm
(
LocalDateTime
localDateTime
)
{
if
(
Objects
.
isNull
(
localDateTime
))
{
try
{
if
(
Objects
.
isNull
(
localDateTime
))
{
return
""
;
}
DateTimeFormatter
formatter
=
DateTimeFormatter
.
ofPattern
(
"yyyy-MM-dd HH:mm"
);
return
localDateTime
.
format
(
formatter
);
}
catch
(
Exception
e
)
{
return
""
;
}
DateTimeFormatter
formatter
=
DateTimeFormatter
.
ofPattern
(
"yyyy-MM-dd HH:mm"
);
return
localDateTime
.
format
(
formatter
);
}
/**
...
...
@@ -76,14 +93,40 @@ public class DateUtil {
* @return
*/
public
static
LocalDateTime
getLocalDateTime
(
String
timeStr
)
{
if
(
StringUtils
.
isBlank
(
timeStr
))
{
try
{
if
(
StringUtils
.
isBlank
(
timeStr
))
{
return
null
;
}
DateTimeFormatter
formatter
=
DateTimeFormatter
.
ofPattern
(
"yyyy/MM/dd HH:mm"
);
// 解析字符串为LocalDateTime
LocalDateTime
localDateTime
=
LocalDateTime
.
parse
(
timeStr
,
formatter
);
return
localDateTime
;
}
catch
(
Exception
e
)
{
return
null
;
}
}
DateTimeFormatter
formatter
=
DateTimeFormatter
.
ofPattern
(
"yyyy/MM/dd HH:mm"
);
// 解析字符串为LocalDateTime
LocalDateTime
localDateTime
=
LocalDateTime
.
parse
(
timeStr
,
formatter
);
return
localDateTime
;
/**
* yyyy/MM/dd 转成 LocalDateTime
* @param timeStr
* @return
*/
public
static
LocalDateTime
getYMDLocalDateTime
(
String
timeStr
)
{
try
{
if
(
StringUtils
.
isBlank
(
timeStr
))
{
return
null
;
}
DateTimeFormatter
formatter
=
DateTimeFormatter
.
ofPattern
(
"yyyy/MM/dd"
);
LocalDate
localDate
=
LocalDate
.
parse
(
timeStr
,
formatter
);
return
localDate
.
atStartOfDay
();
// 转换为当天的开始时间 (00:00)
}
catch
(
Exception
e
)
{
return
null
;
}
}
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
getHHmm
(
LocalDateTime
.
now
()));
}
/**
...
...
@@ -91,10 +134,14 @@ public class DateUtil {
* @return
*/
public
static
Date
convertDateByLocalDateTime
(
LocalDateTime
localDateTime
){
//指定时区(如系统默认时区)
ZoneId
zoneId
=
ZoneId
.
systemDefault
();
//转换为 ZonedDateTime,再转为 Instant,最后生成 Date
Date
date
=
Date
.
from
(
localDateTime
.
atZone
(
zoneId
).
toInstant
());
return
date
;
try
{
//指定时区(如系统默认时区)
ZoneId
zoneId
=
ZoneId
.
systemDefault
();
//转换为 ZonedDateTime,再转为 Instant,最后生成 Date
Date
date
=
Date
.
from
(
localDateTime
.
atZone
(
zoneId
).
toInstant
());
return
date
;
}
catch
(
Exception
e
)
{
return
null
;
}
}
}
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