Java Time zone timezone is inconsistent with the operating system time zone __java

Source: Internet
Author: User
Tags american time

Today, I found a problem with the date function of the Java gadget I just wrote. The specific time is 11 hours slow.

There are no problems checking the XP system's date, time zone, and related options in the registry.

Run the following code

Import Java.util.TimeZone;
Import Java.util.Calendar;
public class test{public
    static void Main (string[] args) throws Exception {
        Calendar calendar = Calendar.getinsta nCE ();      
        System.out.println ("Current time:" + calendar.gettime ());
        System.out.println ("Calendar Time Zone::" + calendar.gettimezone (). GetID ());
        System.out.println ("User.timezone:" + system.getproperty ("User.timezone"));
        System.out.println ("User.country:" + system.getproperty ("User.country"));
        SYSTEM.OUT.PRINTLN ("Default time zone:" + Timezone.getdefault (). GetID ());
    }
The output is:

-----------Output-----------  
Current time: Thu 04:50:19 BRT 2015
calendar time zone:: America/bahia
User.timezone:america/bahia
User.country:CN
default time zone: America/bahia
It's 15:50, it's 11 hours slow, the time zone is America, but the country is China.


To get the calendar instance, use the Settimezone () method to set the time zone to East eight and run the output again as follows:

-----------Output-----------  
Current time: Thu 04:52:58 BRT 2015
calendar time zone:: Asia/shanghai
User.timezone:america/bahia
User.country:CN
default time zone: America/bahia
Although the time zone in the calendar has changed to East Eight, it still shows American time and the country is still in China.

It appears that the output time is related to the default time zone.


Delete the calendar's Settimezone () method, and modify the default time zone in the TimeZone class:

Import Java.util.TimeZone;
Import Java.util.Calendar;
public class test{public
    static void Main (string[] args) throws Exception {
        system.setproperty ("User.timezone" , "Asia/shanghai");
        This is also possible with the following method, but it does not set the User.timezone property. Suggest using the above method
        //Timezone.setdefault (Timezone.gettimezone ("Asia/shanghai"));
        Calendar calendar = Calendar.getinstance ();
        System.out.println ("Current time:" + calendar.gettime ());
        System.out.println ("Calendar Time Zone::" + calendar.gettimezone (). GetID ());
        System.out.println ("User.timezone:" + system.getproperty ("User.timezone"));
        System.out.println ("User.country:" + system.getproperty ("User.country"));
        SYSTEM.OUT.PRINTLN ("Default time zone:" + Timezone.getdefault (). GetID ());
    }

At this point the output:

-----------Output-----------  
Current time: Thu 16:06:17 CST 2015
calendar time zone:: Asia/shanghai
User.timezone:asia/shanghai
User.country:CN
default time zone: Asia/shanghai

Thankfully, it's normal.

The cause of the problem is where. Why my Java default time zone is the United States. Google later found that many people have encountered this bug, generally due to the JDK version behind or the system registry of the time zone is incorrect. But my system setup is no problem, the JDK version is the latest 1.8, is there any disagreement between XP and JDK1.8.

The specific timezone mechanism can be seen in this article:

http://log-cd.iteye.com/blog/368238





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.