Strange Java date

Source: Internet
Author: User

The date issue occurs during eclipse programming as follows.

1. Use settime (date) to assign values to the calendar:

When you assign a value to the calendar using the following method, the result is: "Cal. gettime (). tostring ();" = "Thu Jan 01 08:01:00 CST 1970 ".

 
Calendar Cal = calendar. getinstance (timezone. gettimezone ("GMT"); simpledateformat sdateformat = new simpledateformat ("yyyy-mm-dd hh: mm: SS"); Date = sdateformat. parse ("08:01:00"); Cal. settime (date );

IfCodeChanged:

 
Calendar Cal = calendar. getinstance (timezone. gettimezone ("GMT + 8 "));

The result is still: "Cal. gettime (). tostring (); "=" Thu Jan 01 08:01:00 CST 1970 ". (the time zone has changed. Why is the gettime () result the same ?)

 

2. assign values to the calendar using set (INT, Int, INT:

When you assign a value to the calendar using the following method, the result is "Cal. gettime (). tostring (); "=" Thu Jan 01 16:01:00 CST 1970 ". (the time zone is set to Greenwich mean time, so the display time obtained by gettime is based on the Greenwich Mean Time plus 8 hours, which is changed to the standard Chinese time .)

 
Calendar Cal = calendar. getinstance (timezone. gettimezone ("GMT"); Cal. Set (, 0 );

If you change the first line of code:

 
Calendar Cal = calendar. getinstance (timezone. gettimezone ("GMT + 8 "));

The result is: "Cal. gettime (). tostring (); "=" Thu Jan 01 08:01:00 CST 1970 ". (set the time zone to the China time zone, and gettime () gets the same time as the set time .)

 

Question:

Why do the results of setting timezone to GMT, GMT + 8, or other, Cal. gettime () are the same when you use calendar. settime (date? The result of using calendar. Set (INT, Int, INT) and gettime () is reasonable.

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.