美文网首页
LocalDateTime使用中遇到的问题

LocalDateTime使用中遇到的问题

作者: JavaM | 来源:发表于2019-07-26 11:42 被阅读0次
  1. LocalDateTime使用中遇到的问题
  2. 使用LocalDateTime遇到的问题
  3. Elasticsearch+kibana date索引 东八区问
  4. 微服务通信时遇到的错误记录和解决办法记录
  5. java 8 中List属性去重-redis序列
  6. springboot中LocalDateTime格式问题
  7. LocalDateTime使用
  8. Jackson之LocalDateTime转换
  9. 解决mapstuct中date和LocalDateTime互转时
  10. 5. Java8 新时间 API
    1. LocalDateTime和数据库的精度问题。

    开发中发现每次设置存入数据库的时间为withHour(23). withMinutes(59). withSeconds(59) ;结果存入库有0点0分0秒的情况。经过查看日志发现ms进位了。(可能和数据库类型有关系,忘记了是datetime还是timestamp了,有兴趣的自己看下是不是两个数据类型都会出现这个问题)

    INSERT INTO `coupon_records` (  `id`, `start_time`,  `end_time`)
    VALUES('999','2019-07-26 23:59:59','2019-07-26 23:59:59.876');
    
    SELECT * from `coupon_records` WHERE `coupon_id` = 999;
    
    image

    比如2019年7月26日23点59分59秒876毫秒存到数据库变成了2019年27日0点0分0秒。

    需withHour(23). withMinutes(59). withSeconds(59) .withns(0);

    2.序列化问题

    LocalDateTime用Hession序列化没问题,jackson序列化有问题,。

    JSON parse error: Can not deserialize value of type java.time.LocalDateTime 
    from String "2019-07-26 11:25:10.314": Text '2019-07-26 11:25:10.314' 
    could not be parsed at index 10;
    nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: 
    Can not deserialize value of type java.time.LocalDateTime 
    from String "2019-07-26 11:25:10.314":Text '2019-07-26 11:25:10.314' 
    could not be parsed at index 10 at 
    [Source: java.io.PushbackInputStream@9ea12ad;
    

    原因 LocalDateTime 的字符串形式 是 2019-07-26T11:25:10.314,因为中间有个T,有些序列化没有做特殊处理,故不支持。
    解决方案,对序列化源码进行扩充,编写符合条件的序列化反序列化方式。

    相关文章