The ultimate solution for the difference between the Java acquisition time and the system time is 8 hours
1. Set the time zone before taking the date (add the following code snippet before the date format)
TimeZone tz = TimeZone.getTimeZone(“ETC/GMT-8″);
TimeZone.setDefault(tz);
or (you can test it)
TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
example:
Date date = new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
System.out.println(sdf.format(date ));
2. When formatting the date, add the milliseconds of the current date to the milliseconds of 8 hours
//The following is the method of adding 8 hours to the time. I wrote the method in a MyDate class, public static String formatTimeEight(String time) throws Exception { Date d = null; SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); d = sd.parse(time); // Write the current time as a timestamp using the date.getTime () method, plus the milliseconds corresponding to 8 hours long rightTime = (long) (d.getTime() + 8 * 60 * 60 * 1000); String newtime = sd.format(rightTime);//Format the new timestamp into time format again return newtime; }
3. Open the Registry Editor and add "MapID"="-1,75" to [ HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones\China Standard Time] .
Fourth, modify the startup file (tomcat server)
in the startup.bat file
Modify the JAVA_OPTS variable and add -Duser.timezone=GMT+08 after it.
Personally, I think it is better to use the fourth method, without modifying the code or changing the test system environment. With the fourth method, the problem is solved.
The above solutions are for reference only
http://blog.sina.com.cn/s/blog_6a292d8e0101jc0h.html