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