MySQL 时区

1.查看当前时间:
mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2020-01-08 03:20:05 |
+---------------------+

2.查看时区
mysql> show variables like '%zone%';
+------------------+--------+
| Variable_name    | Value  |
+------------------+--------+
| system_time_zone | UTC    |
| time_zone        | SYSTEM |
+------------------+--------+
time_zone说明MySQL使用system的时区,system_time_zone说明system使用UTC时区

3.修改 time_zone
修改全局 time_zone
mysql> set global time_zone = '+8:00';
Query OK, 0 rows affected (0.00 sec)

修改当前会话 time_zone
mysql> set time_zone = '+8:00';
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like '%time_zone%';
+------------------+--------+
| Variable_name    | Value  |
+------------------+--------+
| system_time_zone | UTC    |
| time_zone        | +08:00 |
+------------------+--------+
2 rows in set (0.00 sec)

4.查看当前时间
mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2020-01-08 11:28:08 |
+---------------------+
1 row in set (0.00 sec)

5. timestamp 类型与时区有关
create table user_zone (
  id int(20) NOT NULL AUTO_INCREMENT,
  created_at datetime NOT NULL,
  birthday timestamp NOT NULL,
  PRIMARY KEY (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2020-01-08 11:35:57 |
+---------------------+

insert into user_zone(created_at, birthday) values(now(), now());

mysql> select * from user_zone;
+----+---------------------+---------------------+
| id | created_at          | birthday            |
+----+---------------------+---------------------+
|  1 | 2020-01-08 11:36:42 | 2020-01-08 11:36:42 |
+----+---------------------+---------------------+

set time_zone = 'UTC';

mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2020-01-08 03:37:46 |
+---------------------+

mysql> select * from user_zone;
+----+---------------------+---------------------+
| id | created_at          | birthday            |
+----+---------------------+---------------------+
|  1 | 2020-01-08 11:36:42 | 2020-01-08 03:36:42 |
+----+---------------------+---------------------+

## java 8 时间类 与 数据库映射

@ToString
@Setter
@Entity
@Table(name = "time_type")
@NoArgsConstructor
public class TimeType {

    private Long id;
    private Instant time;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long getId() {
        return id;
    }

    @Convert(converter = InstantPersistenceConverter.class)
    @Column(name = "time", columnDefinition = "DATETIME")
    public Instant getTime() {
        return time;
    }
}

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import java.time.Instant;
import java.util.Date;

@Converter(autoApply = true)
public class InstantPersistenceConverter implements AttributeConverter<Instant, Date> {

    @Override
    public Date convertToDatabaseColumn(Instant instant) {
        return java.util.Date.from(instant);
    }

    @Override
    public Instant convertToEntityAttribute(Date date) {
        return date.toInstant();
    }
}

mysql> show create table time_type;

| time_type | CREATE TABLE `time_type` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `time` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1 |

mysql> select * from time_type;
+----+---------------------+
| id | time                |
+----+---------------------+
|  1 | 2020-01-08 18:56:41 |
+----+---------------------+

spring-data-jpa 有 Jsr310JpaConverters 进行转化

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值