Android processing of Runtime

Source: Internet
Author: User
Tags time zones

In general, it is very troublesome for us to deal with time. For example, to implement Perpetual calendar, we will use the lunar calendar, the Gregorian calendar, and the lunar calendar ..

There is also the issue of time difference that I want to discuss with you today.

I have previously worked on a global weather + clock application, including the weather and time in more than 30 thousand cities around the world.

The weather and time difference are all easy to say, that is, I am bored by the time of the crash. However, it is also normal to think about it. Isn't the domineering iPhone even stuck in the trouble of the crash not long ago?

 

Generally, we need to deal with such time applications. We must have a piece of data about time difference.

For example, China offset: + 8

We can calculate the current time of China based on the offset and Greenwich Mean Time.

When you enter the region, some regions (note that the region is not a country, while some countries in the United States use the region, but some do not --#). this offset will be increased or decreased by about 1 hour based on whether it is the hour (I remember that some regions entering the hour had a 30-hour deviation)

 

The biggest difficulty now is that you cannot know whether a city is in Beijing !!! At the beginning, I naively thought that if I could get a offset similar to GMT +, I could see whether the region was executed for renewal, but this was a big mistake. as I mentioned above, the same country and even the same region are not sure whether to implement uniform renewal. that is to say, they all belong to a timezone such as GMT +, and some may not use the hour.

I understand that the time zone is divided by longitude, and every 15 degrees is a time zone, so there are exactly 24 time zones in total. At GMT +, then the implementation of the renewal at different latitudes is different

Therefore, the offset (+ 8: 00) obtained from the data cannot directly obtain the usage of the cursor, and this offset is not necessarily correct. It should change with the cursor.

Now let's take a look at the android API:

Timezone. gettimezone (string specifiedid) first has a method related to the problem we want to solve, but the parameter is a string-type specifiedid.

How can I get this specifiedid? We will use string [] specifiedids = timezone. getavailableids (offsetgmt * 60*60*1000 );

The specifiedids array is displayed, and there are many specifiedids such as Asia/Brunei, Asia/choibalsan, Asia/Chongqing ....

According to this specifiedid, we can get the timezone object.

 

 
String [] specifiedids = timezone. getavailableids (offsetgmt * 60*60*1000 );

String timezoneid = timezone. gettimezone (specifiedids [0]). GETID ();

Time time =NewTime (timezoneid );

 

 

 

Then, when Android automatically implements the token for you, the time we get can be used directly and will change with the time when you enter the token.

 

The last question left over is the specifiedid. Here is the first one by default.

Timezone. gettimezone (specifiedids [0]). GETID ();

This is completely inaccurate.

If someone knows a better method, please advise.

 

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.