JVM时区设置
springboot工程运行时,需要指定时区,这样获取的时间才会和系统时间相同。以下介绍方法:
1.查看当前时区
centos7以前(不含centos7)
cat /etc/sysconfig/clock
centos7以后(包含centos7)
timedatectl
在启动jar包的时候指定-Duser.timezone即可。举例:
nohup java -Xms1024m -Xmx10240m -Xmn3840m -Xss512k -Djava.io.tmpdir=./tmp-jar -Duser.timezone=Asia/Shanghai $JAR_NAME >/dev/null 2>&1 &
Mybatis
连接MYSQL数据库时需要指定时区,这样查出来的时间和数据库中的时间才会一致。
具体操作:
spring.datasource.druid.url=jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai&useSSL=false&zeroDateTimeBehavior=convertToNull&useLegacyDatetimeCode=false
注意这里的 serverTimezone=Asia/Shanghai
jdbc.url=jdbc:mysql://localhost:3306/db_name?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&useSSL=false&zeroDateTimeBehavior=convertToNull&useLegacyDatetimeCode=false&rewriteBatchedStatements=true&serverTimezone=GMT%2B8
这里的GMT%2B8
是url编码
这里使用的是阿里的druid连接池。