无论是new Date()还是Calendar.getInstance()获取的都是格林威治时间,而且user.timezone也是GMT,更郁闷的是,同样配置的其他同事却没有这个问题。
网上有些人说改过系统时间或时区后这个问题就能解决,但是我改了,问题依旧。
不知道各位大虾遇到过这个问题没有,是怎么解决的?

解决方案 »

  1.   

    试试Calendar.getInstance(Locale.CHINA)
      

  2.   

    to: justlearn
    时间和时区的设置是没有问题的to: huntor
    还是不行补充:user.timezone是GMT+8这个是没问题的,一开始我描述错了
      

  3.   

    如果系统时区设置是正确的话。那这个问题在 CSDN 上被问过好多次,我也没碰到过,根据其他网友提供的信息估计与 Windows 注册表的设置有关。参考这个帖子:
    http://topic.csdn.net/u/20080923/17/b93f6c68-92f0-4479-bcf0-ac056860b15a.html
      

  4.   

    还是怀疑是JDK版本和操作系统的问题,因为换了JDK6以后就没问题了,但是客户的实际环境是1.5的,所以还是不行
      

  5.   

    你执行一下这段代码,看看输出是什么:Calendar c = Calendar.getInstance();
    System.out.println(c.getTimeZone());如果正常的话,输出应该是:sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null]
      

  6.   

    to bao110908
    谢谢,不过这种方法我已经试过了,改了时区之后,得到的系统时间还是格林威治时间
      

  7.   

    to bao110908sun.util.calendar.ZoneInfo[id="GMT",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]我郁闷啊
      

  8.   

    在命令行输入 java -Duser.timezone=Asia/Jerusalem DateTest 试试
      

  9.   

    那执行一下这几行代码,看看都输出了些什么:System.out.println("user.timezone = " + System.getProperty("user.timezone"));
    System.out.println(" user.country = " + System.getProperty("user.country"));
    System.out.println("    java.home = " + System.getProperty("java.home"));
      

  10.   

    还有你可以进注册表,搜索一下 Time Zones,然后看看china那个时区的数据
      

  11.   

    如果用TimeZone tz = TimeZone.getTimeZone("ETC/GMT-8");
    TimeZone.setDefault(tz);
    然后再获取时间,是可以的,但这只是一个折中的办法,况且其他机器上的客户端如果用这样的代码肯定会出问题的
      

  12.   

    to bao110908,结果如下user.timezone = GMT
    user.country = CN
    java.home = D:\Program Files\Java\jre1.5.0_09就时区有问题,其他都没问题
      

  13.   

    找到Java目录下的依次: \jre\lib\zi\Etc      \jre\lib\zi      \jdk1.6.0_18\jre\lib\zi\Etc      \jdk1.6.0_18\jre\lib\zi下找到GMT文件,删除之(可以备份一下,以防万一),然后复制一份GMT-8并重命名为GMT替换原来的GMT。这样,就达到了掩人耳目的目的。GMT-8在Etc目录下网上找到的方法,不知道可行不
      

  14.   

    还有你如果在命令行运行会有问题吗?还是只是在eclipse下有问题
      

  15.   

    to justlearn
    命令行也有问题,我试一下你的方法
      

  16.   

     Date date =new date();
      String d= date.tolocalString();
       这个d就是正确的
      

  17.   

    还有个办法就是在eclipse的run as的参数设置,VM参数中添加 -Duser.timezone=Asia/Shanghai
    然后运行方法试下,应该就设好了
      

  18.   

    确实啊,从google一些资料来看,最佳解决方案应该还是改注册表,应该是windows更新以后把注册表某些timezone的信息改了
      

  19.   

    试试JDK DST Timezone Update Tool
    http://java.sun.com/javase/tzupdater_README.html
      

  20.   

    那就奇怪了,刚才看了一下 Windows JVM 的源代码,JVM 在初始化时没有设置过 user.timezone 属性:%OPEN_JDK_HOME%/jdk/src/windows/native/java/lang/java_props_md.c : Line.947/* User TIMEZONE */
    {
        /*
         * We defer setting up timezone until it's actually necessary.
         * Refer to TimeZone.getDefault(). However, the system
         * property is necessary to be able to be set by the command
         * line interface -D. Here temporarily set a null string to
         * timezone.
         */
        sprops.timezone = "";
    }另外,JVM 所使用系统时区会通过 Win32 API 的 GetTimeZoneInformation 函数获得:#include <iostream>
    #include <windows.h>using namespace std;int main(int argc, char* argv[]) {
        TIME_ZONE_INFORMATION zoneInfo;
        setlocale(LC_ALL, ".936");
        GetTimeZoneInformation(&zoneInfo);
        wprintf(L"Standard Name: %s\n", zoneInfo.StandardName);
        printf("Bias: %ld\n", zoneInfo.Bias);
    }上面代码,在我机器上输出:Standard Name: 中国标准时间
    Bias: -480
      

  21.   

    Java 的 bug 数据库中也有个类似的问题(Bug#4762673, http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4762673)。根据该 BUG 库中的回复,可以基本上断定不是 Java 中的 bug。
      

  22.   

    看了一个下午的 JVM 的源代码,看得晕死了,很遗憾没有解决。Windows 版本 Java 在获得系统时区除了使用 GetTimeZoneInformation 函数之外,还使用以下三个注册表中的值:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Time Zones
    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\TimeZoneInformationJAVA 使用 TimeZone.getSystemTimeZoneID 这个 private 的方法获得 user.timezone 值的。你把下面的代码执行一下,看看结果是什么:import java.lang.reflect.Method;
    import java.util.TimeZone;public class ZoneInfoTest {    public static void main(String[] str) throws Exception {
            Method getSystemTimeZoneID = TimeZone.class.getDeclaredMethod("getSystemTimeZoneID", String.class, String.class);
            getSystemTimeZoneID.setAccessible(true);
            String javaHome = System.getProperty("java.home");
            String userCountry = System.getProperty("user.country");
            Object obj = getSystemTimeZoneID.invoke(null, javaHome, userCountry);
            System.out.println("user.timezone = " + obj);
        }
    }如果输出的是 null 的话,那看看 System.getProperty("java.home")/jre/lib/tzmappings 文件中有没有这些:Beijing:-1,75::Asia/Shanghai:
    China:-1,75::Asia/Shanghai:
    China Standard Time:-1,75::Asia/Shanghai:tzmappings 是个只读文件。如果我把这三行全部用 # 注释,在我机器上执行 TimeZone.getDefault(); 后,那系统属性 user.timezone 的值也为 GMT
      

  23.   

    好累啊,一个下午的时间全花在这上面了 :(那个 tzmappings 文件必须是 System.getProperty("java.home") 所输出目录 jre/lib 子目录中的文件。
      

  24.   

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones
    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\TimeZoneInformation我是win7,貌似只有这两个,另外那个搜索不到
      

  25.   

    会不会你的 jDk 不是 international 版本没带一些区域设置。
      

  26.   

    to bao110908
    感谢解答按你的代码,输出为null但是那个目录下的tzmapping中,确实包含那三项
      

  27.   


    如果为 null 的话,至止已经完全排除了 Java 的问题,可以断定是 Windows 系统注册表的问题了。看一下系统注册表节点:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones 内部是否有 China Standard Time 子节点。再看一下 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\TimeZoneInformation 节点中 StandardName 值是什么(Windows 6.0 及以上版本的查看 TimeZoneKeyName 值)在 http://jeremymurray.org/doku.php/blog 找到一段批处理程序,你执行一下看看输出是什么(复制成为 now.bat 文件,在控制台中执行):[code=BatchFile]@echo off
    setlocal
    FOR %%A IN (%Date%) do (
        set CURRENTDATE=%%A
    )
     
    for /f "tokens=1,2" %%A in ('time /t') do (
        set CURRENTTIME=%%A %%B
    )set TIMEZONEKEYNAME=
    for /f "tokens=1,2*" %%K in ('reg query HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\TimeZoneInformation') do (
        if %%K==ActiveTimeBias set /a ACTIVETIMEBIAS=%%M
        if %%K==Bias set /a BIAS=%%M
        if %%K==DaylightName set DAYLIGHTTIMEZONE=%%M
        if %%K==StandardName set STANDARDTIMEZONE=%%M
        if %%K==TimeZoneKeyName set TIMEZONEKEYNAME=%%M
    )rem This changed in Vista/Win 7.  The info in the registry are now references to tzres.dll.
    rem Key off of the TimeZoneKeyName in the registry to see if we're on a system that supports it.
    rem This second lookup is because TIMEZONEKEYNAME is always non-DST - this other registry entry gives both.
    if not ("%TIMEZONEKEYNAME%") == ("") for /f "tokens=1,2*" %%K in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones\%TIMEZONEKEYNAME%"') do (
        if %%K==Dlt set DAYLIGHTTIMEZONE=%%M
        if %%K==Slt set STANDARDTIMEZONE=%%M
    )
     
    if ("%ACTIVETIMEBIAS%") == ("%BIAS%") set CURRENTTIMEZONE=%STANDARDTIMEZONE%
    if not ("%ACTIVETIMEBIAS%") == ("%BIAS%") set CURRENTTIMEZONE=%DAYLIGHTTIMEZONE%
     
    echo %CURRENTDATE% %CURRENTTIME% %CURRENTTIMEZONE%
    endlocal[/code]我机器上的输出是:E:\>now
    星期一 12:10  中国标准时间
      

  28.   

    另外,如果是 jdk 1.5 的话,那用 java -version 看一下 java 的版本号,根据 Sun 提供的资料,对应于 Windows 系统,JDK 1.5 最小的版本号:Windows 7, Windows 2008 R2, Windows Vista SP2, Windows 2008 SP2 需要 1.5.0_21 及以后版本
    Windows 2008 需要 1.5.0_19 及以后版本
    Windows Vista 需要 1.5.0_14 及以后版本JDK 1.6 系统支持最小的版本号Windows 7 需要 1.6.0_14 及以后版本
    Windows Server 2008 R2 需要 1.6.0_16 及以后版本你检查一下 JDK 的版本,JDK 5 的输出结果必须是大于等于 1.5.0_21 的。
      

  29.   

    就目前我所知道的而言,输出的时间时区不正确可能的原因:* Windows 系统注册表有问题。如果 32 楼的 now 输出正确的话,那就进入下一步。* 安装的 JDK 版本不支持当前版本的 Windows 系统。如果 JDK 的版本号不能达到 33 楼最小版本的话,就得去安装最新版本的 JDK。
      

  30.   

    to bao110908
    我的JDK是1.5.0_09,不是Win7的最小版本,不过我同事的机器上JDK也是1.5.0_09,没有这个问题。
    注册表也导入了一份,还是没解决
      

  31.   

    最终的解决办法是升级JDK了,无奈之举
      

  32.   

    /*ForTest.java*/
    import java.util.TimeZone;
    import java.util.Date;
    import java.text.DateFormat;public class ForTest {public static void main(String[] args) {DateFormat dateFormatterChina = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM);//格式化输出
    TimeZone timeZoneChina = TimeZone.getTimeZone("Asia/Shanghai");//获取时区
    dateFormatterChina.setTimeZone(timeZoneChina);//设置系统时区
    Date curDate = new Date();//获取系统时间System.out.println(dateFormatterChina.format(curDate));
    }
      

  33.   

    我的方法能解决这个问题,参考资料:http://hi.baidu.com/jjpro/blog/item/2f807ca9a916c8f91e17a29f.html
      

  34.   

    顶一下34L bao110908 ,就是这个问题郁闷的呢。一直时间取得不对。
      

  35.   

    明明就是JDK1.5_04有BUG,根本就是不系统的问题。
    同样的用JDK1.6就显示正常。这个问题我还在解决中
      

  36.   

    这个问题很老火,被我也给遇到了。郁闷。 我的jdk1.6的,工具MyEclipse8.5,系统xp3的,时区获取的时间都是查