| 场景 
 有探索欲的同学,应该会跟我一样,在看JDK源码时,跟到最后,会出现native方法,雷同下面这个方法
 /**     * Gets the platform defined TimeZone ID.     **/    private static native String getSystemTimeZoneID(String javaHome);看到这个native ,阐明已经挖到核心了,到了这一步,还是不清楚是怎么获取系统的默认时区的,那怎么办,JDK代码只能跟到这里。
 转战OpenJDK,源码下载方式:gitee.com/mirrors/ope…
 什么是native
 
 native是一个计算机函数,一个Native Method就是一个Java调用非Java代码的接口。方法的实现由非Java语言实现,好比C或C++。
 native的源码怎么看呢
 
 以**private static native String getSystemTimeZoneID(String javaHome)**为例
 getSystemTimeZoneID方法所在的package java.util.TimeZone;如图所示,找到TimeZone.c下的getSystemTimeZoneID方法
 
 
 /* * Gets the platform defined TimeZone ID */JNIEXPORT jstring JNICALLJava_java_util_TimeZone_getSystemTimeZoneID(JNIEnv *env, jclass ign,                                            jstring java_home, jstring country){    const char *cname;    const char *java_home_dir;    char *javaTZ;    if (java_home == NULL)        return NULL;    java_home_dir = JNU_GetStringPlatformChars(env, java_home, 0);    if (java_home_dir == NULL)        return NULL;    if (country != NULL) {        cname = JNU_GetStringPlatformChars(env, country, 0);        /* ignore error cases for cname */    } else {        cname = NULL;    }    /*     * Invoke platform dependent mapping function     */    javaTZ = findJavaTZ_md(java_home_dir, cname);    free((void *)java_home_dir);    if (cname != NULL) {        free((void *)cname);    }    if (javaTZ != NULL) {        jstring jstrJavaTZ = JNU_NewStringPlatform(env, javaTZ);        free((void *)javaTZ);        return jstrJavaTZ;    }    return NULL;}重点:调用差别平台相关的映射函数
 /*     * Invoke platform dependent mapping function     */    javaTZ = findJavaTZ_md(java_home_dir, cname);去查找findJavaTZ_md方法时,发现存在分别在solaris和windows两个目次下。
 
 查了下这两个目次的差别:
 由于OpenJDK里,Java标准库和部分工具的源码repo(jdk目次)里,BSD和Linux的平台相关源码都是在solaris目次里的。原本Sun JDK的源码里平台相关的目次就是从solaris和windows这两个目次开始的,后来Unix系的平台相关代码全都放在solaris目次下了,共用大部分代码。简单的理解就是:
 window系统下,使用windows目次下编译的JDK代码
 unix系的平台下,使用solaris目次下编译的JDK代码
 了解差别系统下findJavaTZ_md方法执行
 
 windows系统
 
 /* * Detects the platform time zone which maps to a Java time zone ID. */char *findJavaTZ_md(const char *java_home_dir, const char *country){    char winZoneName[MAX_ZONE_CHAR];    char winMapID[MAX_MAPID_LENGTH];    char *std_timezone = NULL;    int  result;    winMapID[0] = 0;    result = getWinTimeZone(winZoneName, winMapID);    if (result != VALUE_UNKNOWN) {        if (result == VALUE_GMTOFFSET) {            std_timezone = _strdup(winZoneName);        } else {            std_timezone = matchJavaTZ(java_home_dir, result,                                       winZoneName, winMapID, country);        }    }    return std_timezone;}解释写得很清楚,获取“Time Zones”注册表中的当前时区
 /* * Gets the current time zone entry in the "Time Zones" registry. */static int getWinTimeZone(char *winZoneName, char *winMapID){...}时区的设置方式:
 
 当时区上的选择值是从哪取到的,上面有说了,是在注册表中取值
 打开注册表 :Regedit-->
 计算机\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones\unix系的平台
 
 findJavaTz_md()方法的解释上写得很清楚了:将平台时区ID映射为Java时区ID
 /* * findJavaTZ_md() maps platform time zone ID to Java time zone ID * using /lib/tzmappings. If the TZ value is not found, it * trys some libc implementation dependent mappings. If it still * can't map to a Java time zone ID, it falls back to the GMT+/-hh:mm * form. `country', which can be null, is not used for UNIX platforms. *//*ARGSUSED1*/char *findJavaTZ_md(const char *java_home_dir, const char *country){    char *tz;    char *javatz = NULL;    char *freetz = NULL;    tz = getenv("TZ");#ifdef __linux__    if (tz == NULL) {#else#ifdef __solaris__    if (tz == NULL || *tz == '\0') {#endif#endif        tz = getPlatformTimeZoneID();        freetz = tz;    }    /*     * Remove any preceding ':'     */    if (tz != NULL && *tz == ':') {        tz++;    }#ifdef __solaris__    if (strcmp(tz, "localtime") == 0) {        tz = getSolarisDefaultZoneID();        freetz = tz;    }#endif    if (tz != NULL) {#ifdef __linux__        /*         * Ignore "posix/" prefix.         */        if (strncmp(tz, "posix/", 6) == 0) {            tz += 6;        }#endif        javatz = strdup(tz);        if (freetz != NULL) {            free((void *) freetz);        }    }    return javatz;}步骤:
 1、使用< Java home>/lib/tzmappings,。如果没有找到"TZ"变量,就举行第2步
 2、 tz = getPlatformTimeZoneID(); 执行Linux特定的映射,如果找到,返回一个时区ID,否则返回null
 【Linux】Centos7修改系统时区timezone方式:
 timedatectl
 修改时区
 timedatectl  set-timezone Asia/Shanghai
 3、对比/etc/localtime与"/usr/share/zoneinfo目次下的文件,如果同等,就返回时区ID,没有则到第4步
 4、返回到GMT
 |