Jdk1.5 time bug solution under visita

Source: Internet
Author: User
Tags bug id dateformat

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.

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.