Java extracts the time in the visita Environment 8 hours earlier than the correct system time, because our time zone is set to Beijing time (that is, GMT + 8)
Use the followingCodeA problem occurs when the time is used:
Calendar c = calendar. getinstance ();
Dateformat df = new simpledateformat ("HH: mm: SS ");
Return DF. Format (C. gettime ());
After the check, it is found that the default time zone set by Java is changed to id = "GMT", while the time zone in Beijing is GMT + 8, so the difference is exactly 8 hours.
There are three solutions::
1. code optimization
Calendar c = calendar. getinstance ();
Dateformat df = new simpledateformat ("HH: mm: SS ");
Timezone zone = new simpletimezone (28800000, "Asia/Shanghai ");
DF. settimezone (zone );
Return DF. Format (C. gettime ());
2. Upgrade JDK.
This bug exists in jdk1.5 with the bug ID 6440819
Http://bugs.sun.com/bugdatabase/view_bug.do? Bug_id = 6440819
This bug has been fixed in jdk1.6:
Http://java.sun.com/javase/6/webnotes/ReleaseNotes.html
3. manually set System Variables
Add User. timezone as follows:
Java.exe ......-duser. timezone = GMT + 08 ......
The third solution is recommended.
Of course, the worst way is to modify the system time or the time zone.