宿主机
查看时区
timedatectl
cat /etc/timezone
ls -alh /etc/localtime
date -R | awk '{print $NF}'
env | grep TZ
更新tzdata
# 更新时区数据库文件
ansible all_nodes -m shell -a "yum install tzdata" -b
修改时区
# change to UTC+8
sudo timedatectl set-timezone Asia/Shanghai
echo 'Asia/Shanghai' | sudo tee /etc/timezone
# change to UTC
sudo timedatectl set-timezone UTC
sudo bash -c "echo 'UTC' > /etc/timezone"
MySQL
修改时区
- change to UTC+8
set global time_zone = '+8:00';
set time_zone = '+8:00';
flush privileges;
- change to UTC
SET @@global.time_zone='+00:00';
SET time_zone = "+00:00";
备注:退出当前链接重新登录(新session) 或 重启mysql进程 后生效
查看时区
show VARIABLES like '%time_zone%';
select now();

show time_zone
FAQ
Java进程时间与宿主机时间不一致
原因:部分地区采取冬令时夏令时政策,且政策有调整,tzdata未及时更新
措施:采用最新版本的JDK
参考资料
【报错】Flask-Apschedul Timezone offset does not match system offset: 0 != 28800. Please, check your_Eff
Debug
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
ZoneId defaultZone = ZoneId.systemDefault();
System.out.println("The default zone:" + defaultZone);
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDateTime = now.atZone(defaultZone).format(formatter);
System.out.println("The current time:" + formattedDateTime);
}
}
java -version && javac Main.java && java Main && timedatectl