V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
blackboom
V2EX  ›  Java

JAVA 获取时间不正确

  •  
  •   blackboom ·
    blackstorm · 2015-08-19 22:15:20 +08:00 · 3349 次点击
    这是一个创建于 3144 天前的主题,其中的信息可能已经有所发展或是发生改变。
    今天部署项目时遇到问题
    linux 的系统时间是正确的
    通过 new date 获取的时间比正确时间早了 8 个小时
    程序中有大量的 new date 语句

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

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

    怎么修改调整为正确的时间呢?
    第 1 条附言  ·  2015-09-12 23:51:06 +08:00
    17 条回复    2015-08-20 17:37:43 +08:00
    nopy
        1
    nopy  
       2015-08-19 22:20:58 +08:00   ❤️ 1
    把系统时间加 8 个小时。
    unique
        2
    unique  
       2015-08-19 22:22:02 +08:00   ❤️ 1
    重新装一次 JDK
    blackboom
        3
    blackboom  
    OP
       2015-08-19 22:23:50 +08:00
    @nopy 系统时间是正确的
    tianice
        4
    tianice  
       2015-08-19 22:27:38 +08:00   ❤️ 1
    phx13ye
        5
    phx13ye  
       2015-08-19 22:30:28 +08:00   ❤️ 1
    硬件时间同步成 UTC , 自己的环境时区随便怎么搞
    beyondsoft
        6
    beyondsoft  
       2015-08-19 22:38:57 +08:00   ❤️ 1
    设置这个关键变量试试
    TZ='Asia/Shanghai'; export TZ
    blackboom
        7
    blackboom  
    OP
       2015-08-19 22:39:05 +08:00
    @tianice 这个方法不行
    KentY
        8
    KentY  
       2015-08-19 22:44:38 +08:00   ❤️ 1
    你启动你的 server 的时候是不是有个 java_opts -Duser.timezone=xxxx ? 导致你的 server 运行在 xxxx timezone., 你的程序里 new Date ()就是那个时区的了.
    KentY
        9
    KentY  
       2015-08-19 22:45:33 +08:00   ❤️ 1
    @nopy 系统时间+8 小时, 是万不能采用的办法.
    blackboom
        10
    blackboom  
    OP
       2015-08-19 22:55:30 +08:00
    @unique
    @tianice
    @phx13ye
    @beyondsoft
    @KentY
    感谢各位 在 tomcat 中添加了 JAVA_OPTS="-Duser.timezone=GMT+08" 时间恢复正常
    Tomcat 程序时间是正常了,但不是根本的解决办法,其他 JAVA 程序依旧时间不正常
    kn007
        11
    kn007  
       2015-08-19 22:55:38 +08:00
    LS 正解
    phx13ye
        12
    phx13ye  
       2015-08-19 23:14:24 +08:00   ❤️ 1
    blackboom
        13
    blackboom  
    OP
       2015-08-19 23:22:43 +08:00
    @phx13ye 不是 而是 linux

    java 程序获取时间是 “ UTC 协调世界时”
    系统时间是 “ CST 格林尼治标准时间 ”
    anexplore
        14
    anexplore  
       2015-08-19 23:36:42 +08:00   ❤️ 1
    看一下 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
        15
    aaronmix  
       2015-08-20 00:36:30 +08:00   ❤️ 1
    JDK 版本多少?
    codeyung
        16
    codeyung  
       2015-08-20 13:30:45 +08:00   ❤️ 1
    还是 linux 系统时间要改 UTC 试试 然后 tomcat 启动里添加既可
    sovalvo
        17
    sovalvo  
       2015-08-20 17:37:43 +08:00   ❤️ 1
    我是来看一楼的😄
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2768 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 12:01 · PVG 20:01 · LAX 05:01 · JFK 08:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.