JAVA 获取时间不正确

2015 年 8 月 19 日
 RRL
今天部署项目时遇到问题
linux 的系统时间是正确的
通过 new date 获取的时间比正确时间早了 8 个小时
程序中有大量的 new date 语句

安装 JDK 时系统时区是错误的,在后来修改正确的系统时间(上海)

现在 JAVA 不能获取正确的时间

怎么修改调整为正确的时间呢?
4629 次点击
所在节点    Java
17 条回复
nopy
2015 年 8 月 19 日
把系统时间加 8 个小时。
unique
2015 年 8 月 19 日
重新装一次 JDK
RRL
2015 年 8 月 19 日
@nopy 系统时间是正确的
tianice
2015 年 8 月 19 日
phx13ye
2015 年 8 月 19 日
硬件时间同步成 UTC , 自己的环境时区随便怎么搞
beyondsoft
2015 年 8 月 19 日
设置这个关键变量试试
TZ='Asia/Shanghai'; export TZ
RRL
2015 年 8 月 19 日
@tianice 这个方法不行
KentY
2015 年 8 月 19 日
你启动你的 server 的时候是不是有个 java_opts -Duser.timezone=xxxx ? 导致你的 server 运行在 xxxx timezone., 你的程序里 new Date ()就是那个时区的了.
KentY
2015 年 8 月 19 日
@nopy 系统时间+8 小时, 是万不能采用的办法.
RRL
2015 年 8 月 19 日
@unique
@tianice
@phx13ye
@beyondsoft
@KentY
感谢各位 在 tomcat 中添加了 JAVA_OPTS="-Duser.timezone=GMT+08" 时间恢复正常
Tomcat 程序时间是正常了,但不是根本的解决办法,其他 JAVA 程序依旧时间不正常
kn007
2015 年 8 月 19 日
LS 正解
phx13ye
2015 年 8 月 19 日
RRL
2015 年 8 月 19 日
@phx13ye 不是 而是 linux

java 程序获取时间是 “ UTC 协调世界时”
系统时间是 “ CST 格林尼治标准时间 ”
eycfsjd
2015 年 8 月 19 日
看一下 java.util.TimeZone 的获取 timezone 的源码吧,
private static synchronized TimeZone setDefaultZone () {
TimeZone tz;
// get the time zone ID from the system properties
String zoneID = AccessController.doPrivileged (
new GetPropertyAction ("user.timezone"));

// if the time zone ID is not set (yet ), perform the
// platform to Java time zone ID mapping.
if (zoneID == null || zoneID.isEmpty ()) {
String country = AccessController.doPrivileged (
new GetPropertyAction ("user.country"));
String javaHome = AccessController.doPrivileged (
new GetPropertyAction ("java.home"));
try {
zoneID = getSystemTimeZoneID (javaHome, country );
if (zoneID == null ) {
zoneID = GMT_ID;
}
} catch (NullPointerException e ) {
zoneID = GMT_ID;
}
}}
。。。。。。。。。。
}
aaronmix
2015 年 8 月 20 日
JDK 版本多少?
codeyung
2015 年 8 月 20 日
还是 linux 系统时间要改 UTC 试试 然后 tomcat 启动里添加既可
sovalvo
2015 年 8 月 20 日
我是来看一楼的😄

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/214401

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX