Android essay--android time, date related classes and methods

Source: Internet
Author: User

Today is about time, date related classes and methods in Android. In Android, the time-and date-related classes have three classes, such as the Times, Calendar, and date. DateFormat and SimpleDateFormat, which are related to the date format output, today will mention a little bit about the usage of these two classes and will take a minute to talk about it next time.

  

First, Time class

In the official API, it is suggested that the use of time instead of calendar, for unknown reasons, is said to use time on the CPU load is relatively small.

In time, month and day when the escape character corresponds to %y%m%d%h%m%s, remember, the case must not write wrong, otherwise the output is completely wrong.

Well, not to say anything nonsense, this or to see the code than I told the truth

1  PackageCom.example.alarmmanager;2 3 Importandroid.app.Activity;4 ImportAndroid.os.Bundle;5 ImportAndroid.text.format.Time;6 7  Public classMainactivityextendsActivity {8 9     protected voidonCreate (Bundle savedinstancestate) {Ten         Super. OnCreate (savedinstancestate); One  A         //The initial session is a time object, which can also be written as: "Gtm+8", that is, add the timezone -Time time =NewTime (); -         //set the current time the Time.settonow (); -         //get current time zone - System.out.println (Time.getcurrenttimezone ()); -         //Output Current Date +System.out.println (Time.year + "year" + Time.month + "Month" +Time.monthday -+ "Day" + Time.hour + "when" + Time.minute + "min" + time.second + "seconds" ++ ": Now is the year of the first" + Time.yearday + "Day"); A         //gets the first week of the year that is currently atSystem.out.println ("Now is the first of the Year" + time.getweeknumber () + "Week"); -         //format session output current date, time -System.out.println (Time.format ("%y-%m-%d%h:%m:%s")); -System.out.println (Time.format ("%y%m%dt%h%m%s"));//Same as time.format2445 -          -System.out.println (time.format2445 ());//shaped like 20140829t143523 in          -         //time.format3339 (Boolean AllDay). If Allday is true, the output y-m-d format to         //Output y-m-d-t-h-m-s UTC If Allday is false and the time zone is UTC +         //otherwise output y-m-d-t-h-m-s +-GMT -System.out.println (time.format3339 (true)); the          *         //Parse date string, "20081013t160000z", "20081013" $System.out.println (Time.parse ("20081013t160000"));Panax Notoginseng          -         //outputs the current time as Yyyymmddthhmmss the         //The output is related to time.parse, not in detail, the Android API has a detailed explanation + System.out.println (time.tostring ()); A  the     } +  -}

The output results are as follows:

Second, Calendar class

The Calendar class is an underlying abstract class used for conversions between a Date object and some integer fields, such as year, month, day, time, minute, and second. The Date object is precision in milliseconds and is used to represent a specific moment.

In Android, 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. Note that this is not the same as date and SimpleDateFormat, and the timestamp obtained by date and SimpleDateFormat does not take into account the time zone, but rather gets the standard GMT timestamp. However, the time stamp difference between the two can be obtained by using the Timezone.getdefault (). Getrawoffset () method.

Some of the constant fields used in the calendar:

calendar.year--year calendar.month--month calendar.date--date calendar.day_of_month--date, and the above field has exactly the same meaning calendar.hour-- 12 Hour calendar.hour_of_day--24 hour calendar.minute--minutes calendar.second--seconds calendar.day_of_week--Days

Below, or paste the code, all the content to be said in the code of the comments, while looking at the edge of understanding easier

1  PackageCom.example.alarmmanager;2 3 ImportJava.text.SimpleDateFormat;4 ImportJava.util.Calendar;5 Importjava.util.Date;6 7 ImportAndroid.annotation.SuppressLint;8 Importandroid.app.Activity;9 ImportAndroid.os.Bundle;Ten ImportAndroid.text.format.DateFormat; One  A@SuppressLint ("SimpleDateFormat") -  Public classMainactivityextendsActivity { -  the     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); -  -         //get a Calendar instance +Calendar Calendar =calendar.getinstance (); -         //output date information, and a lot of constant fields, I don't write anymore. +System.out.println (Calendar.get (calendar.year) + "year" A+ Calendar.get (calendar.month) + "Month" at+ Calendar.get (calendar.day_of_month) + "Day" -+ Calendar.get (calendar.hour_of_day) + "Time" -+ Calendar.get (calendar.minute) + "min" -+ Calendar.get (Calendar.second) + "seconds" + "\ n today is the Week" -+ Calendar.get (Calendar.day_of_week) + "Is this year's first" -+ Calendar.get (calendar.week_of_year) + "Week"); in         //formatted output date, in this method, the time display is 12 hours, if you need to display a 24-hour system, just change HH to KK -System.out.println (Dateformat.format ("Yyyy-mm-dd kk:mm:ss", to calendar.gettime ()). ToString ()); +         //Convert a date to milliseconds -         Try { the             //returns the Date object for this calendar *Date calendardate =calendar.gettime (); $             //YYYY-MM-DD KK:MM:SS formatted Calendardate object and converted to string objectPanax NotoginsengString time =DateFormat -. Format ("Yyyy-mm-dd kk:mm:ss", Calendardate). toString (); theSystem.out.println ("DateFormat after formatting string value:" +Time ); +             //Create a new SimpleDateFormat object, the format of the time ASimpleDateFormat format =NewSimpleDateFormat ( the"Yyyy-mm-dd Kk:mm:ss"); +             //Format.parse () returns the data type of a date -Date Date =Format.parse (time); $             //returns the number of milliseconds from 1970-01-01 00:00:00 to date representing the time $System.out.println ("Date.gettime:" +date.gettime ()); -             //Format.parse () returns the data type of a date -System.out.println ("Format.parse ():" the+ Format.parse ("2014-08-29 15:56:00")); -}Catch(Exception e) {Wuyi e.printstacktrace (); the         } -  Wu     } -  About}

About the time, date-related classes and methods in Android so much, if you find something missing, I hope you put forward, I will continue to complement this blog. Next to learn about DateFormat and simpledateformat knowledge, good night to fill up! Oh yes ~

  

Author: Sky Road

Reprint please indicate source: http://www.cnblogs.com/travellife/

Android essay--android time, date related classes and methods

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.