mysql中的毫秒问题

来源:互联网 发布:海尔燃气热水器 知乎 编辑:程序博客网 时间:2024/06/06 09:04

一. 注意点

0. 数据库版本的选定

    5.6.4以上

1.  数据库jdbc驱动版本的选定

    5.6.4以上版本数据库对应的jdbc驱动, 如果驱动版本过低, 会把毫秒给截断, 出现灵异事件(明明程序中指定了毫秒, 但插入到数据库时反而没有毫秒了)

2.  数据库字段的选取

     DateTime(3/6)或者Timestamp(3/6)

3.  jdbcType的选取

     TIMESTAMP

4.  javaType的选取

      java.util.Date

二. mysql中DateTime和Timestamp

DateTime
  a. 8个字节储存(8 bytes storage)
  b. 实际格式储存(Just stores what you have stored and retrieves the same thing which you have stored.)
  c. 与时区无关(It has nothing to deal with the TIMEZONE and Conversion.)
        d. 存储的时间范围为:'1000-01-01 00:00:00.000000' 到 '9999-12-31 23:59:59.999999'

Timestamp
  a. 4个字节储存(Time stamp value is stored in 4 bytes)
  b. 值以UTC格式保存( it stores the number of milliseconds)
  c. 时区转化 ,存储时对当前的时区进行转换,检索时再转换回当前的时区。
        d. 存储的时间范围为:'1970-01-01 00:00:01.000000' 到 '2038-01-19 03:14:07.999999'

实例对比
  a. 先插入一个数据insert into `t8` values(now(), now());
  b. 改变客户端时区(东9区,日本时区)。
  c. 再次显示插入的数据,变化了,timestamp类型的数据 增加了 1个小时

0 0
原创粉丝点击