Android essay-Android time, date-related classes and Methods

Source: Internet
Author: User

Android essay-Android time, date-related classes and Methods
Today we will talk about time and date-related classes and methods in Android. In Android, classes related to Time and Date mainly include Time, Calendar, and Date. The DateFormat and SimpleDateFormat related to the date formatting output will be mentioned a little bit today about the usage of these two classes. I will give you a detailed explanation next time. I. In the official API of Time, it is recommended that Time be used instead of Calendar. The reason is unknown. It is said that the CPU load is relatively small when Time is used. In Time, the escape characters of year, month, day, hour, minute, and second correspond to % Y % m % d % H % M % S respectively. Remember that the escape characters must be case-insensitive. Otherwise, the output is completely incorrect. Well, I don't want to talk about anything nonsense. This is based on how the code is actually compared to what I 've talked about, copying code 1 package com. example. alarmmanager; 2 3 import android. app. activity; 4 import android. OS. bundle; 5 import android. text. format. time; 6 7 public class MainActivity extends Activity {8 9 protected void onCreate (Bundle savedInstanceState) {10 super. onCreate (savedInstanceState); 11 12 // an initial Time object can also be written as: Time time = new Time ("GTM + 8 "), add the Time zone 13 time = new Time (); 14 // Set the current time to 15 time. setToNow (); 16 // get the current time zone 17 System. out. println (Time. getCurrentTimezone (); 18 // output the current date 19 System. out. println (time. year + "year" + time. month + "month" + time. monthDay20 + "day" + time. hour + "hour" + time. minute + "minute" + time. second + "second" 21 + ": Now the first time of the year" + time. yearDay + "day"); 22 // obtain the WEEK 23 of the current year. out. println ("now in the year" + time. getWeekNumber () + "Week"); 24 // format output current date, time 25 System. out. println (tim E. format ("% Y-% m-% d % H: % M: % S"); 26 System. out. println (time. format ("% Y % m % dT % H % M % S"); // corresponds to time. format2445 same 27 28 System. out. println (time. format2445 (); // such as 20140829T14352329 30 // time. format3339 (boolean allDay ). If allDay is true, output Y-M-D format 31 // If allDay is false and time zone is UTC, output Y-M-D-T-H-M-S UTC32 // otherwise output Y-M-D-T-H-M-S +-GMT33 System. out. println (time. format3339 (true); 34 35 // parses the date string, "20081013t1620.z", "20081013" 36 System. out. println (time. parse ("20081013T160000"); 37 38 // output 39 Results and time at the current time in YYYYMMDDTHHMMSS. for more information about parse, the Android API provides a detailed explanation of 40 System. out. println (time. toString (); 41 42} 43 44} the output result of the copied code is as follows: 2. Cale The ndar Calendar class is a basic abstract class used for conversion between the Date object and Some integer fields (such as year, month, day, hour, minute, and second. The Date object is precise in milliseconds to indicate a specific time point. 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. Note that this is different from Date and SimpleDateFormat. The timestamp obtained by Date and SimpleDateFormat is used to obtain the standard GMT timestamp instead of the time zone. However, the timestamp difference between the two can be obtained by using the TimeZone. getDefault (). getRawOffset () method. Some common constant fields in the Calendar: copy the code Calendar. YEAR -- YEAR Calendar. MONTH -- MONTH Calendar. DATE -- DATE Calendar. DAY_OF_MONTH -- date, which has the same meaning as the preceding field Calendar. HOUR Calendar in the HOUR--12-HOUR format. HOUR_OF_DAY--24 hour Calendar ar. MINUTE -- MINUTE Calendar. SECOND -- seconds Calendar. DAY_OF_WEEK-copy the code in the day of the week and paste the code. All the content to be discussed is written in the comments of the Code. It is easier to copy the package com. example. alarmmanager; 2 3 import java. text. simpleDateFormat; 4 import java. util. calendar; 5 import java. util. date; 6 7 import android. annotation. suppressLint; 8 import android. app. activity; 9 import android. OS. bundle; 10 import android. text. format. dateFormat; 11 12 @ SuppressLint ("SimpleDateFormat") 13 public class MainActivity extends Activity {14 15 protected void onCreate (Bundle savedInstanceState) {16 super. onCreate (savedInstanceState); 17 18 // get the Calendar instance 19 Calendar ar calendar = Calendar ar. getInstance (); 20 // output date information, there are many constant Fields, I will not write 21 System. out. println (calendar. get (Calendar. YEAR) + "YEAR" 22 + calendar. get (Calendar. MONTH) + "MONTH" 23 + calendar. get (Calendar. DAY_OF_MONTH) + "day" 24 + calendar. get (Calendar. HOUR_OF_DAY) + "Hour" 25 + calendar. get (Calendar. MINUTE) + "MINUTE" 26 + calendar. get (Calendar. SECOND) + "SECOND" + "\ n today is the week" 27 + calendar. get (Calendar. DAY_OF_WEEK) + "this year's" 28 + calendar. get (Calendar. WEEK_OF_YEAR) + "Week"); 29 // format the output date. In this method, the time is displayed in the 12-hour format. If you need to display the 24-hour format, replace hh with kk30 System. out. println (DateFormat. format ("yyyy-MM-dd kk: mm: ss", 31 calendar. getTime ()). toString (); 32 // convert the Date to a Date in milliseconds 33 try {34 // return the Date object of this calendar 35 Date calendarDate = calendar. getTime (); 36 // formatted the calendarDate object by yyyy-MM-dd kk: mm: ss and converted to String object 37 String time = DateFormat38. format ("yyyy-MM-dd kk: mm: ss", calendarDate ). toString (); 39 System. out. println ("String value after DateFormat formatting:" + time); 40 // create a SimpleDateFormat object, the time format is 41 SimpleDateFormat format = new SimpleDateFormat (42 "yyyy-MM-dd kk: mm: ss"); 43 // format. parse () returns the Data Type of a Date 44 Date date = format. parse (time); 45 // return the number of milliseconds from 00:00:00 to date representing the time 46 System. out. println ("date. getTime: "+ date. getTime (); 47 // format. parse () returns the Data Type of a Date 48 System. out. println ("format. parse (): "49 + format. parse ("15:56:00"); 50} catch (Exception e) {51 e. printStackTrace (); 52} 53 54} 55 56}

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.