Recently, we encountered an event with incorrect Java time. The phenomenon is as follows:
Obtain the system time using a Java statement.
Date d = new date ();
Simpledateformat SDF = new simpledateformat ("yyyy-mm-dd KK: mm: SS ");
System. Out. println (SDF. Format (d ));
Or:
Calendar now = calendar. getinstance ();
System. Out. println (now. Get (calendar. hour_of_day ));
System. Out. println (now. Get (calendar. Minute ));
System. Out. println (now. Get (calendar. Second ));
The obtained time is always eight hours less than the system time, and the obtained time is always Greenwich mean time (0 Time Zone). Check whether the system time is correct. The time zone is GMT + 8: 00 Beijing ...... And so on. However, this type of problem has not occurred before. The time was correct before, but the Java time problem occurred after the system was reinstalled. It may be that the system time registry encountered a problem, the Registry will not be modified here. The Default Time Zone of myeclipse or Java is 0, so:
Considering the portability of the Code, the current time zone is initialized wherever the time is used. Add the Code:
Settimezone (timezone. gettimezone ("Asia/Shanghai "));
Modify as follows:
Date d = new date ();
Simpledateformat SDF = new simpledateformat ("yyyy-mm-dd KK: mm: SS ");
SDF. settimezone (timezone. gettimezone ("Asia/Shanghai "));
System. Out. println (SDF. Format (d ));
Or:
Calendar now = calendar. getinstance ();
Now. settimezone (timezone. gettimezone ("Asia/Shanghai"); // important
System. Out. println (now. Get (calendar. hour_of_day ));
System. Out. println (now. Get (calendar. Minute ));
System. Out. println (now. Get (calendar. Second ));
In this way, you can.
========================================================== ========================================================== ==============
To address this problem, we have mentioned that you can add the following sentence before time initialization when the system code is used, but this is troublesome. Therefore, after installing Java JDK, you can replace several source files for the same purpose.
Settimezone (timezone. gettimezone ("Asia/Shanghai "));
Another method:
Find
In sequence:Jdk1.60/JRE/lib/Zi/etc
Jdk1.60/JRE/lib/Zi
Java/jre1.6.0 _ 14/JRE/lib/Zi/etc
Java/jre1.6.0 _ 14/JRE/lib/Zi
Find the GMT file, delete it (you can back up, just in case), then copy a GMT-8 and rename it to GMT to replace the original GMT. In this way, the goal is to hide your ears and ears. Try it to solve it!