springboot mysql date时间的处理

2022年6月5日09:45:17

在数据库中创建时间戳字段

··· sql
CREATE TABLEmytest (
text varchar(255) DEFAULT ‘’ COMMENT ‘内容’,
create_time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT ‘创建时间’,
update_time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT ‘更新时间’
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
···
springboot mysql date时间的处理

参考:MySql的CURRENT_TIMESTAMP

注解的使用

因为mysql中的数据是timestamp类型的,因此如果你用map映射的话,就必须添加上格式的注解。

@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")private Date update_time;//更新时间@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")private Date create_time;//创建时间

这里没有加上时区

参考:springboot关于mysql数据库date时间的坑
参考:SpringBoot + MySQL

时间戳格式的转换

//获取时间戳,从1970-01-01起的毫秒数
            long t_longValue = create_time.getTime();
// 得到:1604671436000

参考:java实现Date和Timestamp的相互转换

  • 作者:蓝黑墨水
  • 原文链接:https://blog.csdn.net/lanxiaziyi/article/details/109531199
    更新时间:2022年6月5日09:45:17 ,共 779 字。