parent
1dac2aff03
commit
d19876cb46
11 changed files with 250 additions and 36 deletions
@ -0,0 +1,9 @@ |
||||
package org.springblade.desk.util.json.constant; |
||||
|
||||
public interface JsonPattern { |
||||
|
||||
/** |
||||
* 固定日期格式 |
||||
*/ |
||||
String DATE_PATTERN = "yyyy-MM-dd"; |
||||
} |
||||
@ -0,0 +1,64 @@ |
||||
package org.springblade.desk.util.json.deserializer; |
||||
|
||||
import cn.hutool.core.date.DateTime; |
||||
import cn.hutool.core.date.DateUtil; |
||||
import com.fasterxml.jackson.core.JsonParser; |
||||
import com.fasterxml.jackson.databind.DeserializationContext; |
||||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer; |
||||
import org.springblade.desk.util.json.constant.JsonPattern; |
||||
|
||||
import java.io.IOException; |
||||
import java.time.LocalDateTime; |
||||
import java.util.Date; |
||||
|
||||
/** |
||||
* 自定义LocalDateTime反序列化器:将yyyy-MM-dd字符串转为当天开始时间(00:00:00)的Date对象 |
||||
*/ |
||||
public class LocalDateTimeBeginOfDayDeserializer extends StdDeserializer<LocalDateTime> { |
||||
|
||||
|
||||
/** |
||||
* 无参构造(Jackson要求) |
||||
*/ |
||||
public LocalDateTimeBeginOfDayDeserializer() { |
||||
this(null); |
||||
} |
||||
|
||||
/** |
||||
* 带类型构造 |
||||
* |
||||
* @param vc |
||||
*/ |
||||
public LocalDateTimeBeginOfDayDeserializer(Class<?> vc) { |
||||
super(vc); |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @param jsonParser Parser used for reading JSON content |
||||
* @param ctx Context that can be used to access information about |
||||
* this deserialization activity. |
||||
* @return |
||||
* @throws IOException |
||||
*/ |
||||
@Override |
||||
public LocalDateTime deserialize(JsonParser jsonParser, DeserializationContext ctx) throws IOException { |
||||
// 获取JSON中的字符串值(如:2026-01-01)
|
||||
String dateStr = jsonParser.getText().trim(); |
||||
// 空值处理(避免空指针)
|
||||
if (dateStr.isEmpty() || dateStr.equals("null")) { |
||||
return null; |
||||
} |
||||
try { |
||||
// hutool解析字符串为DateTime
|
||||
DateTime dateTime = DateUtil.parse(dateStr, JsonPattern.DATE_PATTERN); |
||||
// 转为当天开始时间(00:00:00)
|
||||
DateTime beginOfDay = DateUtil.beginOfDay(dateTime); |
||||
// 转换为java.time.LocalDateTime
|
||||
return beginOfDay.toLocalDateTime(); |
||||
} catch (Exception e) { |
||||
// 解析失败时抛出友好异常
|
||||
throw new IOException("日期格式错误,仅支持yyyy-MM-dd格式(如:2026-01-01)", e); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,63 @@ |
||||
package org.springblade.desk.util.json.deserializer; |
||||
|
||||
import cn.hutool.core.date.DateTime; |
||||
import cn.hutool.core.date.DateUtil; |
||||
import com.fasterxml.jackson.core.JsonParser; |
||||
import com.fasterxml.jackson.databind.DeserializationContext; |
||||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer; |
||||
import org.springblade.desk.util.json.constant.JsonPattern; |
||||
|
||||
import java.io.IOException; |
||||
import java.time.LocalDateTime; |
||||
|
||||
/** |
||||
* 自定义LocalDateTime反序列化器:将yyyy-MM-dd字符串转为当天结束时间(23:59:59)的Date对象 |
||||
*/ |
||||
public class LocalDateTimeEndOfDayDeserializer extends StdDeserializer<LocalDateTime> { |
||||
|
||||
|
||||
/** |
||||
* 无参构造(Jackson要求) |
||||
*/ |
||||
public LocalDateTimeEndOfDayDeserializer() { |
||||
this(null); |
||||
} |
||||
|
||||
/** |
||||
* 带类型构造 |
||||
* |
||||
* @param vc |
||||
*/ |
||||
public LocalDateTimeEndOfDayDeserializer(Class<?> vc) { |
||||
super(vc); |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @param jsonParser Parser used for reading JSON content |
||||
* @param ctx Context that can be used to access information about |
||||
* this deserialization activity. |
||||
* @return |
||||
* @throws IOException |
||||
*/ |
||||
@Override |
||||
public LocalDateTime deserialize(JsonParser jsonParser, DeserializationContext ctx) throws IOException { |
||||
// 获取JSON中的字符串值(如:2026-01-01)
|
||||
String dateStr = jsonParser.getText().trim(); |
||||
// 空值处理(避免空指针)
|
||||
if (dateStr.isEmpty() || dateStr.equals("null")) { |
||||
return null; |
||||
} |
||||
try { |
||||
// hutool解析字符串为DateTime
|
||||
DateTime dateTime = DateUtil.parse(dateStr, JsonPattern.DATE_PATTERN); |
||||
// 转为当天结束时间(23:59:59.999,不影响Oracle查询)
|
||||
DateTime beginOfDay = DateUtil.endOfDay(dateTime); |
||||
// 转换为java.time.LocalDateTime
|
||||
return beginOfDay.toLocalDateTime(); |
||||
} catch (Exception e) { |
||||
// 解析失败时抛出友好异常
|
||||
throw new IOException("日期格式错误,仅支持yyyy-MM-dd格式(如:2026-01-01)", e); |
||||
} |
||||
} |
||||
} |
||||
Loading…
Reference in new issue