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
afd48a78
Commit
afd48a78
authored
Feb 02, 2026
by
zhangxingmin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
push
parent
80a2ac11
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
72 additions
and
13 deletions
+72
-13
yd-common/src/main/java/com/yd/common/utils/CustomLocalDateTimeDeserializer.java
+72
-13
No files found.
yd-common/src/main/java/com/yd/common/utils/CustomLocalDateTimeDeserializer.java
View file @
afd48a78
...
...
@@ -7,31 +7,89 @@ import com.fasterxml.jackson.databind.JsonDeserializer;
import
java.io.IOException
;
import
java.time.LocalDateTime
;
import
java.time.format.DateTimeFormatter
;
import
java.time.format.DateTimeParseException
;
import
java.time.format.DateTimeFormatterBuilder
;
import
java.time.temporal.ChronoField
;
public
class
CustomLocalDateTimeDeserializer
extends
JsonDeserializer
<
LocalDateTime
>
{
private
static
final
DateTimeFormatter
DATE_FORMATTER
=
DateTimeFormatter
.
ofPattern
(
"yyyy-MM-dd"
);
private
static
final
DateTimeFormatter
DATE_TIME_FORMATTER
=
DateTimeFormatter
.
ofPattern
(
"yyyy-MM-dd HH:mm:ss"
);
// 定义多种可能的格式
private
static
final
DateTimeFormatter
[]
FORMATTERS
=
{
// ISO 格式: yyyy-MM-dd'T'HH:mm:ss
DateTimeFormatter
.
ISO_LOCAL_DATE_TIME
,
// 标准格式: yyyy-MM-dd HH:mm:ss
DateTimeFormatter
.
ofPattern
(
"yyyy-MM-dd HH:mm:ss"
),
// 只有日期: yyyy-MM-dd
DateTimeFormatter
.
ofPattern
(
"yyyy-MM-dd"
),
// 其他可能的格式
DateTimeFormatter
.
ofPattern
(
"yyyy/MM/dd HH:mm:ss"
),
DateTimeFormatter
.
ofPattern
(
"yyyy/MM/dd"
),
DateTimeFormatter
.
ofPattern
(
"yyyy.MM.dd HH:mm:ss"
),
DateTimeFormatter
.
ofPattern
(
"yyyy.MM.dd"
)
};
@Override
public
LocalDateTime
deserialize
(
JsonParser
p
,
DeserializationContext
ctxt
)
throws
IOException
{
String
text
=
p
.
getText
();
if
(
text
==
null
||
text
.
isEmpty
())
{
if
(
text
==
null
||
text
.
trim
().
isEmpty
())
{
return
null
;
}
t
ry
{
// 先尝试解析完整的时间格式
return
LocalDateTime
.
parse
(
text
,
DATE_TIME_FORMATTER
);
}
catch
(
DateTimeParseException
e1
)
{
t
ext
=
text
.
trim
();
// 尝试用所有格式解析
for
(
DateTimeFormatter
formatter
:
FORMATTERS
)
{
try
{
// 如果失败,尝试解析日期格式并添加时间
return
LocalDateTime
.
parse
(
text
+
" 00:00:00"
,
DATE_TIME_FORMATTER
);
}
catch
(
DateTimeParseException
e2
)
{
// 如果都失败,抛出异常
throw
new
IOException
(
"Failed to parse LocalDateTime from: "
+
text
,
e2
);
// 如果是日期格式,转换为 LocalDateTime
if
(
formatter
.
toString
().
contains
(
"yyyy-MM-dd"
)
&&
!
formatter
.
toString
().
contains
(
"HH:mm:ss"
))
{
// 这是纯日期格式,需要转换为 LocalDateTime
return
LocalDateTime
.
parse
(
text
+
"T00:00:00"
,
DateTimeFormatter
.
ISO_LOCAL_DATE_TIME
);
}
return
LocalDateTime
.
parse
(
text
,
formatter
);
}
catch
(
Exception
e
)
{
// 继续尝试下一个格式
continue
;
}
}
// 如果所有格式都失败,尝试更灵活的解析
return
parseFlexible
(
text
);
}
private
LocalDateTime
parseFlexible
(
String
text
)
throws
IOException
{
try
{
// 尝试处理带T的日期时间
if
(
text
.
contains
(
"T"
))
{
// 移除可能的多余空格
text
=
text
.
replace
(
" "
,
""
);
// 确保T后面有时间部分
if
(
text
.
indexOf
(
'T'
)
+
1
<
text
.
length
())
{
String
timePart
=
text
.
substring
(
text
.
indexOf
(
'T'
)
+
1
);
if
(
timePart
.
isEmpty
())
{
text
=
text
+
"00:00:00"
;
}
else
if
(
timePart
.
split
(
":"
).
length
==
2
)
{
text
=
text
+
":00"
;
}
return
LocalDateTime
.
parse
(
text
,
DateTimeFormatter
.
ISO_LOCAL_DATE_TIME
);
}
}
// 尝试处理只有日期的格式
if
(
text
.
length
()
==
10
&&
text
.
charAt
(
4
)
==
'-'
&&
text
.
charAt
(
7
)
==
'-'
)
{
return
LocalDateTime
.
parse
(
text
+
"T00:00:00"
,
DateTimeFormatter
.
ISO_LOCAL_DATE_TIME
);
}
// 尝试处理包含空格的日期时间
if
(
text
.
length
()
>=
10
&&
text
.
charAt
(
4
)
==
'-'
&&
text
.
charAt
(
7
)
==
'-'
)
{
if
(
text
.
length
()
>
10
&&
text
.
charAt
(
10
)
==
' '
)
{
// 格式: yyyy-MM-dd HH:mm:ss
return
LocalDateTime
.
parse
(
text
,
DateTimeFormatter
.
ofPattern
(
"yyyy-MM-dd HH:mm:ss"
));
}
}
throw
new
IOException
(
"无法解析日期时间: "
+
text
);
}
catch
(
Exception
e
)
{
throw
new
IOException
(
"无法解析日期时间: "
+
text
,
e
);
}
}
}
\ 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