Reproduced from: http://blog.liaoxiaoqi.com /? P = 492
There are three common date operations in Android:
- Date type
- Calendar type
- 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.