LocalDateTime序列化总结

版权说明: 本文由优快云博主keep丶原创,转载请保留此块内容在文首。
原文地址: https://blog.youkuaiyun.com/qq_38688267/article/details/146703276

1.背景

  现平台中存在两种日期类型数据,一种为精度为秒的,一种为精度为毫秒的。目前存在三个序列化场景:WEB接口、Excel文件IO、Mysql数据交互。
  这三个场景的序列化方式各不相同,现需要将这些场景中的序列化方式统一为'yyyy-MM-dd HH:mm:ss.SSS' 'yyyy-MM-dd HH:mm:ss'
  本文将从概念、总体方案及各场景实现方案等方面详细介绍LocalDateTime序列化事项,本文适合开发人员查阅。

2.序列化介绍

  序列化是将对象转换为可传输或存储的格式(如JSON、字符串、二进制等),反序列化则是将序列化后的数据恢复为原始对象。

常见场景

  • Web接口返回数据时,将LocalDateTime转换为特定格式的字符串。
  • 数据库交互时,处理时间字段的读写格式(基于MyBatis/MyBatis-Plus)。
  • Excel导出时,格式化日期时间字段。

关键问题

  • 默认格式不符合需求(如T字符需要替换为空格)。
  • 需支持毫秒和非毫秒两种格式的兼容处理。

3.总体方案

  • **目标:**统一处理时间字段格式,避免T字符,支持毫秒与非毫秒格式。
  • 规则:
    • 序列化:
      若字段不需要毫秒,格式化为yyyy-MM-dd HH:mm:ss
      若字段需要毫秒,格式化为yyyy-MM-dd HH:mm:ss.SSS
    • 反序列化:
      根据字符串长度自动匹配格式:
      长度19字符:yyyy-MM-dd HH:mm:ss
      长度23字符:yyyy-MM-dd HH:mm:ss.SSS
      其他情况使用hutools的DateUtil.formatDateTime()方法处理。

4.各场景实现方式

WEB接口

  • web接口默认序列化方式为Jackson,其序列化工具为ObjectMapper。
  • 注册自定义ObjectMapper实现自定义LocalDateTime序列化。
  • 也可以通过@JsonFormat注解实现特例处理。

    @Bean
    @Primary
    public ObjectMapper objectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        JavaTimeModule javaTimeModule = new JavaTimeModule();
        // 注册自定义序列化器
        javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer());
        // 反序列化规则
        javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(
                new DateTimeFormatterBuilder()
                        .appendPattern("yyyy-MM-dd[ ][HH:mm:ss][.SSS]")
                        .toFormatter()
        ));
        objectMapper.registerModule(javaTimeModule);
        // 禁用时间戳格式
        objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        // 不输出空值字段
        // objectMapper.setDefaultPropertyInclusion(JsonInclude.Include.NON_NULL);
        // 忽略未知字段
        objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        return objectMapper;
    }

EasyExcel

easyExcel有其自身的序列化方式,通过实现其Convert接口并注册来实现自定义序列化方式。

            EasyExcel.write(response.getOutputStream()).head(head(data.getHeadMap())).sheet(data.getFileName())
                    //日期转换器
                    .registerConverter(new LocalDateConverter())
                    //时间转换器
                    .registerConverter(new LocalDateTimeConverter())
                    .doWrite(dataList(data.getDataList(), data.getDataStrMap()));
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

public class LocalDateTimeConverter implements Converter<LocalDateTime> {

    @Override
    public Class<LocalDateTime> supportJavaTypeKey() {
        return LocalDateTime.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public LocalDateTime convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        return DateUtils.parseLocalDateTime(cellData.getStringValue());
    }

    @Override
    public CellData<LocalDateTime> convertToExcelData(LocalDateTime dateTime, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        return new CellData<>(DateUtils.formatLocalDateTime(dateTime));
    }
}

Mybatis/MybatisPlus

mybatis的序列化方式基于TypeHandler,mybatis和mybatis plus都有默认各类型TypeHandler,通过注册自定义TypeHandler来实现自定义序列化方式。

Mybatis
  • 在resultMap中指定typeHandler以实现自定义反序列化:
    <result typeHandler="org.apache.ibatis.type.BigDecimalTypeHandler"/>

  • 在SQL中指定日期格式以实现自定义序列化:

    • INSERT INTO table_name (date_column) VALUES (DATE_FORMAT(#{dateParam}, '%Y-%m-%d %H:%i:%s'))
    • INSERT INTO table_name (date_column) VALUES (#{dateParam,typeHandler=com.example.CustomDateTypeHandler})
Mybatis Plus
  • 配置通用TypeHandler
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

public class LocalDateTimeTypeHandler extends BaseTypeHandler<LocalDateTime> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, LocalDateTime parameter, JdbcType jdbcType)
            throws SQLException {
        ps.setString(i, DateUtils.formatLocalDateTime(parameter));
    }

    @Override
    public LocalDateTime getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String date = rs.getString(columnName);
        return DateUtils.parseLocalDateTime(date);
    }

    @Override
    public LocalDateTime getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String date = rs.getString(columnIndex);
        return DateUtils.parseLocalDateTime(date);
    }

    @Override
    public LocalDateTime getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        String date = cs.getString(columnIndex);
        return DateUtils.parseLocalDateTime(date);
    }
}

  • 需要配置扫描路径才能生效在这里插入图片描述

  • @TableField注解指定typeHandler:
    在这里插入图片描述

5.工具类封装

在DateUtil中封装了统一LocalDateTime序列化方法,平台中统一使用。



    /**
     * 格式化LocalDateTime
     * <p>
     * 参考其toString()方法,修改逻辑以符合系统需求:
     * 2020-10-10 00:00:00.000000001 --> 2020-10-10 00:00:00.000
     * 2020-10-10 00:00              --> 2020-10-10 00:00:00
     * 2020-10-10 00:00:00.001       --> 2020-10-10 00:00:00.001
     * 2020-10-10 00:00:00           --> 2020-10-10 00:00:00
     *
     * @author zzf
     * @date 2025-04-18 15:12
     */
    public static String formatLocalDateTime(LocalDateTime dateTime) {
        if (dateTime == null) {
            return null;
        }
        int yearValue = dateTime.getYear();
        int monthValue = dateTime.getMonthValue();
        int dayValue = dateTime.getDayOfMonth();

        int absYear = Math.abs(yearValue);
        StringBuilder buf = new StringBuilder(28);
        if (absYear < 1000) {
            if (yearValue < 0) {
                buf.append(yearValue - 10000).deleteCharAt(1);
            } else {
                buf.append(yearValue + 10000).deleteCharAt(0);
            }
        } else {
            if (yearValue > 9999) {
                buf.append('+');
            }
            buf.append(yearValue);
        }
        buf.append(monthValue < 10 ? "-0" : "-")
                .append(monthValue)
                .append(dayValue < 10 ? "-0" : "-")
                .append(dayValue);

        buf.append(StringPool.SPACE);

        int hourValue = dateTime.getHour();
        int minuteValue = dateTime.getMinute();
        int secondValue = dateTime.getSecond();
        int nanoValue = dateTime.getNano();
        buf.append(hourValue < 10 ? "0" : "").append(hourValue)
                .append(minuteValue < 10 ? ":0" : ":").append(minuteValue)
                .append(secondValue < 10 ? ":0" : ":").append(secondValue);
        if (nanoValue > 0) {
            // 平台最多只展示到毫秒级
            buf.append(StringPool.DOT);
            buf.append(Integer.toString((nanoValue / 1000_000) + 1000).substring(1));
        }
        return buf.toString();
    }

    public static LocalDateTime parseLocalDateTime(String localDateTimeStr) {
        if (StrUtil.isBlank(localDateTimeStr)) {
            return null;
        }
        // 2020-10-10 00:00:00  长度为 19
        if (localDateTimeStr.length() == 19) {
            return DateUtil.parseLocalDateTime(localDateTimeStr);
            // 2020-10-10 00:00:00.000 长度为 23
        } else if (localDateTimeStr.length() == 23) {
            return DateUtil.parseLocalDateTime(localDateTimeStr, NORM_DATETIME_MS_PATTERN);
        } else {
            return LocalDateTimeUtil.parse(localDateTimeStr);
        }
    }

6.反思和总结

  • 封装设计时,需要考虑序列化统一的问题,避免由于配置不完整导致的问题。
  • 当遇到序列化行为不符合预期时,需要分析定位序列化方式,然后通过更换序列化工具或自定义序列化器来解决。
  • 尽量统一序列化方案或规则,避免在不同场景下序列化规则不一致导致的系统问题。
  • 时间类型对象和精度尽量统一,避免为了兼容和适配导致的问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值