Commit afd48a78 by zhangxingmin

push

parent 80a2ac11
......@@ -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;
}
try {
// 先尝试解析完整的时间格式
return LocalDateTime.parse(text, DATE_TIME_FORMATTER);
} catch (DateTimeParseException e1) {
text = 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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment