Differences Between calendar and date in Android and how to eliminate the impact of Time Zone on date operations

Source: Internet
Author: User

Reproduced from: http://blog.liaoxiaoqi.com /? P = 492

 

There are three common date operations in Android:

    1. Date type
    2. Calendar type
    3. UNIX Timestamp

Among them, Unix timestamps are the most convenient and flexible in computing, and the efficiency is also high; while date and calendar are more convenient in some specific date computing. The simpledateformat class is often used for formatting during date conversion, including converting a specific format string to a date object and formatting a date object to a specific format string.

First, compare the differences between date and calendar. People who have used the date to Unix timestamp may encounter a problem, that is, there is a difference between the timestamp obtained by date or simpledateformat and the timestamp obtained by calendar, if you use the China time zone, the difference is 28800000 ms, that is, 8 hours. Obviously, the difference between these eight hours is due to the time zone, and if the development is closely related to the date and timeProgramIgnore this time difference, it is likely to produce many incredible errors and results. In Android, calendar can automatically adjust the timestamp based on the time zone set by the mobile phone, that is, the real timestamp of the time zone; the timestamp obtained by date and simpledateformat is the standard GMT timestamp instead of the time zone. The timestamp difference between the two can be obtained by using the timezone. getdefault (). getrawoffset () method. Now we can easily find a solution to the time difference between date, simpledateformat, and calendar. A brief description is as follows:

Calendar calendar = calendar. getinstance (); // Get current Calendar Object  
Long Unixtime = calendar. gettimeinmillis (); // Obtain the timestamp corresponding to the date and time in the current time zone.
Long Unixtimegmt = unixtime-timezone. getdefault (). getrawoffset (); // Obtain the timestamp corresponding to the date and time under the standard Greenwich Mean Time

Date = New Date (); // Get the current date object
Unixtimegmt = unixtime = date. gettimeinmillis (); // Obtain the timestamp corresponding to the date and time in the current time zone.

Simpledateformat format = New Simpledateformat ("yyyy-mm-dd hh: mm: SS "); // Set format
String datestring = "03:36:25 "; // Set a date string with the specified format
Unixtimegmt = unixtime = format. Format (date ); // Obtain the timestamp corresponding to the date and time in the current time zone.

Obviously, timestamps must be unified during development to avoid many embarrassing problems. So in the actual development process, should we use the mobile phone to specify the time stamp of the time zone or the standard time stamp? I personally think that the standard timestamp should be used, because the user may change the time zone. If the timestamp corresponding to the time zone is used and the timestamp is saved to the database as a mark, once the time zone changes, the stored data will be faulty with the current time zone settings. using the standard time stamp can avoid this problem, because the program can easily convert the timestamp into a standard timestamp, and the standard timestamp is fixed, so that even if the time zone is modified, the date and time can also be correctly processed.

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.