最近项目遇到一个坑,就是server和db之间存在时区问题,本人的db是utc时间,

可以使用代码设置时区来解决,本人这里使用joda三方包,joda蛮好用的,具体用法这里不做详细描述。

先引入pom

<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.1</version>
</dependency>

下面在使用的地方加上下面一句即可。

// 这里使用UTC,还有其他的配置参数,比如: Asia/Shanghai   Asia/Chongqin  等等
DateTime utcDT = new DateTime().withZone(DateTimeZone.UTC).toLocalDateTime().toDateTime(); ...
...
... UserDto dto = new UserDto();
dto.setCreated(utcDT.toDate());
userMapping.insertRecords(dto);

除了上面的方法外,还可以在驱动器的连接URL上加参数,如下:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/xxx?useUnicode=true&characterEncoding=utf8
&allowMultiQueries=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=111111
说明:useLegacyDatetimeCode=false 关键是这个参数,
引用:http://www.voidcn.com/article/p-eqvuhiio-bsb.html 这里有详细的介绍

附上mysql官网文档:https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html

mysql遇到时区问题的坑(Java解决方案)的更多相关文章

  1. MySql数据库时区异常,java.sql.SQLException: The server time zone value '?й???׼ʱ?' is unrecognized or represents more than one time zone.

    JDBC访问MySql异常 Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException ...

  2. mysql中Incorrect string value乱码问题解决方案

    mysql中Incorrect string value乱码问题解决方案   你是否遇到过类似以下错误? java.sql.SQLException: Incorrect string value: ...

  3. centos7 修改时区,同步时间,Mysql修改时区

    查看时区 timedatectl status [root@localhost nova-back]# timedatectl status Local time: Thu 2019-05-23 15 ...

  4. 入坑Java的自学之路

    # 入坑Java的自学之路 ## 基础知识 - 编程语言:Java python c- 基本算法- 基本网络知识 tcp/ip http/https- 基本的设计模式 ------ ## 工具方面 - ...

  5. paip.c++ 转换 java 解决方案

    paip.c++ 转换 java 解决方案 作者Attilax  艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net/a ...

  6. mysql设置时区方法

    set global time_zone = '+2:00'; ##修改mysql全局时区 set time_zone = '+2:00'; ##修改当前会话时区 flush privileges; ...

  7. 修改MySQL的时区

    修改MySQL的时区,涉及参数time_zone 首先需要查看mysql的当前时区,用time_zone参数 ? 1 2 3 4 5 6 7 8 9 [html] mysql> show var ...

  8. MySQL修改时区的方法小结

    这篇文章主要介绍了MySQL修改时区的方法,总结分析了三种常见的MySQL时区修改技巧,包括命令行模式.配置文件方式及代码方式,需要的朋友可以参考下 方法一:通过mysql命令行模式下动态修改 1.1 ...

  9. [转]MySQL修改时区的方法小结

    本文转自:https://www.cnblogs.com/mracale/p/6064447.html 这篇文章主要介绍了MySQL修改时区的方法,总结分析了三种常见的MySQL时区修改技巧,包括命令 ...

随机推荐

  1. postgrelsql 的 wm_concat : string_agg

    string_agg,array_agg 这两个函数的功能大同小异,只不过合并数据的类型不同 array_agg(expression) 把表达式变成一个数组 一般配合 array_to_string ...

  2. UIView的intrinsicContentSize方法,在按钮中重写

    场景,一个有图片有文字的按钮在其父控件上加了上左的约束,然后,有需要要调整按钮与图片之间的间距,第一想法就是设置title insets 的left. 然后问题出现了,这个按钮的文字居然显示不全了,也 ...

  3. [LeetCode] 259. 3Sum Smaller 三数之和较小值

    Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...

  4. [LeetCode] 278. First Bad Version 第一个坏版本

    You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...

  5. 通过cmd命令控制台关闭已经打开的端口号

    通过cmd命令控制台关闭已经打开的端口号 在出现的窗口里面输入 netstat -ano, 就会出现所有的端口号, Local Address下面的是端口号, PID就是某程序占用的进程号, 这个进程 ...

  6. Java中缓存的介绍

    一.什么是缓存1.Cache是高速缓冲存储器 一种特殊的存储器子系统,其中复制了频繁使用的数据以利于快速访问2.凡是位于速度相差较大的两种硬件/软件之间的,用于协调两者数据传输速度差异的结构,均可称之 ...

  7. objectARX2010及其以上版本使用publish打印(发布)图纸,后台布局打印图纸例子浅析

    AutoCAD 2010版本开始新增了一个发布图纸的功能,可以后台打印图纸,以下是ADN官方博客例子浅析 原文地址 https://adndevblog.typepad.com/autocad/201 ...

  8. nginx实现tcp负载均衡

    1 安装支持库 yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel yum install pcre-d ...

  9. 超实用的 JavaScript 代码片段( ES6+ 编写)

    Array 数组 Array concatenation (数组拼接) 使用 Array.concat() ,通过在 args 中附加任何数组 和/或 值来拼接一个数组. const ArrayCon ...

  10. pandas再次学习

    numpy.scipy官方文档  pandas官方网站  matplotlib官方文档 一.数据结构 二.数据处理 1.数据获取(excel文件数据基本信息) #coding=utf-8 import ...