Use of the time class for Android development

Source: Internet
Author: User

Three ways to use Android date operations:

1.Date

2.Calendar

3.Unix time Stamp

1 and 2 in the calculation of the specific date convenience high, 3 calculation on the convenience and flexibility, high efficiency. The conversion of strings to date objects can be achieved using SimpleDateFormat.

The difference between date and calendar

Calendar is able to automatically adjust the timestamp based on the time zone set by the phone, that is, the time zone's true timestamp calendar.

The time stamp obtained by date and SimpleDateFormat does not take into account the time zone, but instead obtains the standard GMT timestamp.

Timezone.getdefault (). Getrawoffset () can get the time stamp difference between the phone time zone and GMT

This means that if you want to convert the current timestamp to a standard timestamp, you can use the following code

Calendar calendar = Calendar.getinstance (); // gets the current calendar object long unixtime = Calendar.gettimeinmillis (); // gets the timestamp for the date time in the current time zone long unixtimegmt = Unixtime-timezone.getdefault (). Getrawoffset (); // gets the time stamp corresponding to the date time of the standard GMT

Then in the actual development process, the use of the standard timestamp, because the user is likely to change the time zone, if the time stamp of the corresponding time zone is used, and the timestamp as a token amount in the database, then once the time zone changes, the existing data will be a problem with the current time zone settings , using a standard timestamp avoids this problem because the program can easily convert the timestamp to a standard timestamp, and the standard timestamp is fixed so that the datetime can be handled correctly even in the case of a modified time zone.

Android Development gets the current Android date and time of day and seconds

Android files are recommended to replace calendar with time. The load on the CPU with time is small. is especially important when writing widgets.

//The result looks like the actual time of the phoneTime time =NewTime ();//Get current TimeTime.settonow ();//get the individual values of the timeintYear =time.year;//the range of the month is 0~11intmonth =Time.month;intDay =Time.monthday;intminute =Time.minute;inthour =Time.hour;intSEC =Time.second; LOG.V ("Datedemo", "Current time:" + year + "years" + month + "Month" + Day + "days" +Hour+ "Time" + Minute + "min" + sec + "SEC");//Convert dates to a specific formatString timestr = Time.format ("%Y%m%d%H%m%s"); LOG.V ("Datedemo", timestr);

Gets the Android system time is 24-hour or 12-hour system

this = Settings.System.getString (CV,        android.provider.Settings.System.TIME_12_24);

The imported package is Android.provider.Settings

Reference Links:

Http://www.2cto.com/kf/201207/139551.html

http://blog.csdn.net/yudajun/article/details/7939552

Use of the time class for Android development

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.