V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
MySQL 5.5 Community Server
MySQL 5.6 Community Server
Percona Configuration Wizard
XtraBackup 搭建主从复制
Great Sites on MySQL
Percona
MySQL Performance Blog
Severalnines
推荐管理工具
Sequel Pro
phpMyAdmin
推荐书目
MySQL Cookbook
MySQL 相关项目
MariaDB
Drizzle
参考文档
http://mysql-python.sourceforge.net/MySQLdb.html
anzu
V2EX  ›  MySQL

MySQL Timestamp 不能直接接收整型

  •  
  •   anzu · 2021-04-27 17:22:01 +08:00 · 1052 次点击
    这是一个创建于 1086 天前的主题,其中的信息可能已经有所发展或是发生改变。

    字段:begin_at timestamp;

    JPA:private Timestamp beginAt;

    程序运行环境本地时间与数据库本地时间不同。

    begin_at 是前端传来的时间戳,但是在保存的时候 JPA 将它转换成了本地时间(+0 时区)字符串表示,插入数据库的时候,数据库将该字符串再次转换成时间戳,然而使用的是+8 时区进行转换,导致最终保存在数据库的时间戳少了 8 小时。

    Java 日志类似如下:

    org.hibernate.SQL                        : insert into table (begin_at) values (?)
    o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [TIMESTAMP] - [2021-04-27 16:33:07.645]
    

    我以为是 JPA 捣乱,试着直接在 MySQL 敲命令:

    insert into table (begin_at) values (1619514153)

    报错:

    Incorrect datetime value: '1619514153' for column 'begin_at'

    原本想用时间戳统一表示时间,消除因不同时区导致的时间问题,结果 MySQL 的 timestamp 字段不能直接接收整型,坑啊啊啊啊

    brader
        1
    brader  
       2021-04-27 17:24:46 +08:00
    timestamp 接受一个时间字符串。
    建议用 int 吧,我做了这么多项目,都是一直用的 int
    yogogo
        2
    yogogo  
       2021-04-27 17:24:52 +08:00
    我都是用 int
    liprais
        3
    liprais  
       2021-04-27 17:26:53 +08:00
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4197 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 46ms · UTC 05:29 · PVG 13:29 · LAX 22:29 · JFK 01:29
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.