Differences Between Calendar and Date in Android and how to eliminate the impact of Time Zone on Date operations

Source: Internet
Author: User

Differences Between Calendar and Date in Android and how to eliminate the impact of Time Zone on Date operations

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. If this time difference is ignored when developing programs closely related to the date and time, 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 (); // gets the 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 in the standard Greenwich Mean Time.
Date 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 the format
String dateString = "03:36:25"; // you can specify a date String.
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.

 

2. Obtain the time of year, month, hour, minute, and second of the current Android system in Android development.

For Android files, we recommend that you use Time instead of Calendar. The CPU load with Time is small. This is especially important when writing widgets.

Time t = new Time (); // or Time t = new Time ("GMT + 8"); plus the Time Zone information.

Package ***;

Import android. app. Activity;
Import android. OS. Bundle;
Import android. text. format. Time;
Import android. widget. TextView;

Public class ShowTime extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

TextView myTextView = (TextView) findViewById (R. id. myTextView );
Time time = new Time ("GMT + 8 ");
Time. setToNow ();
Int year = time. year;
Int month = time. month;
Int day = time. monthDay;
Int minute = time. minute;
Int hour = time. hour;
Int sec = time. second;
MyTextView. setText ("current time:" + year +
"Year" + month +
"Month" + day +
"Day" + hour +
"Hour" + minute +
"Points" + sec +
"Seconds ");
}
}
The only disadvantage is that the retrieval time is only in 24-hour mode.

========================================================== ========================================================== ==========

How to obtain whether the Android system is in the 24-hour or 12-hour format
ContentResolver cv = this. getContentResolver ();
String strTimeFormat = android. provider. Settings. System. getString (cv,
Android. provider. Settings. System. TIME_12_24 );

If (strTimeFormat. equals ("24 "))

 

{
Log. I ("activity", "24 ");
} Www.2cto.com

Use Calendar to obtain year, month, day, hour, minute, and second
Calendar c = Calendar. getInstance ();
Obtain the system date: year = c. get (Calendar. YEAR)
Month = c. get (Calendar. MONTH)
Day = c. get (Calendar. DAY_OF_MONTH)
Obtain the system time: hour = c. get (Calendar. HOUR_OF_DAY );
Minute = c. get (Calendar. MINUTE)

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.