Problems and solutions of time zone conversion to daylight saving time based on Java

I Preparation knowledge

1.America/New_ The daylight saving time in York is as follows: left not right

2016-3-13,02:00:00 to 2016-11-6,02:00:00

2017-3-12,02:00:00 to 2017-11-5,02:00:00

2. Three letter time zone ID

To work with JDK 1.1 X compatible, and some three letter time zone IDS (such as "PST", "CTT", "ast") are also supported.

However, their use is obsolete because the same abbreviations are often used in multiple time zones

For example, CST: it has four meanings: American, Australian, Chinese and Cuban time

3. Standards

GMT: green mean time Greenwich mean time, which was used as the standard time reference before 1960 GMT + 12 -- > gmt-12

The range of java8 is GMT + 18 -- > gmt-18

UTC: coordinated universal time coordinated world time, which is more accurate than GMT and became a new standard on January 1, 1972; UTC,UTC+1,UTC+2... UTC+12,UTC-12... UTC-1

Java8 range utc-18 -- > UTC + 18

DST: daylight saving time refers to setting the clock forward by one hour in summer to advance the use of sunlight. It is called daylight saving time in Britain;

At present, more than 110 countries use daylight saving time;

In China, it was only implemented for six years from 1986 to 1992 and then cancelled; The reasons are as follows:

1. China has a large East-West span, and adopts the unified Dongba District, which cannot be compatible with the East and West with daylight saving time; 2. In high latitudes, the time of day and night varies greatly in winter and summer; Not significant;

4. It can be expressed as GMT + 8 or etc / GMT-8 (on the contrary, why, because PHP developers think that Dongba district is 8 hours faster than the standard time and 8 hours should be subtracted, so it is expressed like this. Different reference objects lead to different representation methods;)

5. Representation of China's time zone

GMT + 8 UTC + 8 Asia / Harbin Harbin / / China Standard Time Asia / Chongqing Chongqing / / China Standard Time Asia / Chungking Chongqing / / China Standard Time Asia / Urumqi Urumqi / / China Standard Time Asia / Shanghai (area 8 in the East) / / PRC Asia / Macau Macao / / China standard time Hong Kong / / Hong Kong time is consistent with China Standard Time Asia / hong_kong Asia / Taipei Taipei (Taiwan) / / China standard time Singapore is the same as China's time; Asia / Singapore Singapore

6. Representation of standard time zone

UTC UTC + 0 utc-0 GMT Greenwich mean time GMT 0 Greenwich mean time etc / GMT + 0 Greenwich mean time etc / gmt-0 Greenwich mean time etc / gmt-0 Greenwich mean time etc / GMT 0 Greenwich mean time note: GMT + XX (- XX) is very inclusive and can automatically identify the representation of various times

II Time zone conversion

Environment: before java8

1. Convert the current time to the specified time zone display

2. Convert the specified time to the specified time zone display

Can you really convert correctly? There seems to be a pit. I looked at the implementation on the Internet

There's something wrong with daylight saving time

Summary: the results of the above three implementation methods have some problems with daylight saving time

3、 Implementation of Java 8

1. First look at the time zone changes supported by Java 8

2. How to add time zone information, or convert

3. How to obtain the specified time of the current time (omitted in the test)

4. How to get the specified time in the specified time zone

Start to realize the time zone problem left above

Similarly, first look at whether the daylight saving time judgment method using java8 is correct

//Test it and find that the daylight saving time method of java8 is completely correct

Start implementing:

Version 1:

The tests are as follows:

So now I wonder whether this result is a systematic calculation problem, or I don't understand the customs of New York?

However, I can still get the results I want, using two methods:

withEarlierOffsetAtOverlap(),withLaterOffsetAtOverlap()

Version 2:

Test: OK

result:

America/New_ Standard time zone corresponding to York: gmt-5 destination time zone America / New_ York time: 01:59:59, March 13, 2016

-------------

America/New_ Standard time zone corresponding to York: gmt-5 destination time zone America / New_ Time in York: 03:00:00, March 13, 2016

-------------

America/New_ Standard time zone corresponding to York: gmt-5 destination time zone America / New_ Time in York: 02:59:59, November 6, 2016

-------------

America/New_ Standard time zone corresponding to York: gmt-5 destination time zone America / New_ Time in York: 02:00:00, November 6, 2016

-------------

The above problem and solution of converting daylight saving time based on JAVA time zone is all the content shared by Xiaobian. I hope it can give you a reference and support more programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>