Java (myeclipse) 8-hour Solution

Source: Internet
Author: User

 

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!

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.