Android platform related classes and methods for time and date (Date/time)

Source: Internet
Author: User
Tags add time dateformat time and date

1. The time class , which can be given a specific date/time and timezone, can be converted between a date/time format and a string and milliseconds.

(it is said that using the time class has less load on the CPU, but I have not compared it with other types)

Time t = new Time (); Get the initial date time: 19700101t000000asia/shanghai (0,0,0,-1,-28800)

Time T=new time ("gmt+8"); Add Time zone information

T.settonow (); Get current date Time: 20140103t153540asia/shanghai (5,2,28800,0,1388734540)

String str = T.format ("%y%m%dt%h%m%s"); 20140103t155737

Equivalent to the t.format2445 () method.

Parse a string in a date/time format:

T.parse ("20101010t151133");

str = t.format2445 (); 20101010t121133

If it is a string other than this format, the result throws an exception timeformatexception

T.parse ("20101010151133"); Abnormal

T.parse ("aaa201010ddt10151133ddd");//exception

T.parse ("20101010"); Only dates are normal.
Timestr = t.format2445 (); 20101010

Gets the individual domain values for time:

int year = T.year; 2010
int month = T.month; 9 (0-11) It is worth noting here
int date = T.monthday; 10
int hour = T.hour; 15, 24-hour system (0-23)
int minute = T.minute; 11
int second = T.second; 33

2. Calendar class

Dateformat.format (DateFormat, calendar) method, generally used in the format is yyyy mm DD hh mm SS, in this method, the time display is 12-hour system, if need to display 24-hour system, the HH can be replaced with KK.

String out = Dateformat.format ("Mm-dd hh:mm:ss", Calendar.getinstance (). GetTime ()). ToString (); 01-04 11:23:56
out = Dateformat.format ("Yy-mm-dd hh:mm:ss", Calendar.getinstance (). GetTime ()). ToString (); Yy-01-dd 11:25:48 (Incorrect parameters)
out = Dateformat.format ("yyyy-m-d hh:mm:ss", Calendar.getinstance (). GetTime ()). ToString (); yyyy-1-d 11:26:21 (Incorrect parameters)

out = Dateformat.format ("Yy-mm-dd hh:mm:ss", Calendar.getinstance (). GetTime ()). ToString (); 14-01-04 11:59:12
out = Dateformat.format ("yyyy-m-d hh:mm:ss", Calendar.getinstance (). GetTime ()). ToString (); 2014-1-4 03:02:53

out = Dateformat.format ("yyyy-m-d kk:mm:ss", Calendar.getinstance (). GetTime ()). ToString (); 2014-1-4 15:03:19

To get the various domain values for the calendar:

int year = Calendar.getinstance (). get (Calendar.year);
int month = Calendar.getinstance (). Get (Calendar.month) + 1;
int day = Calendar.getinstance (). get (Calendar.day_of_month);
int hour = Calendar.getinstance (). get (Calendar.hour_of_day);
int minute = Calendar.getinstance (). get (Calendar.minute);
int second = Calendar.getinstance (). get (Calendar.second);
out = String.Format ("%04d-%02d-%02d", year, month, day); 2014-01-04
out = String.Format ("%02d:%02d:%02d", hour, minute, second); 11:27:13

Get date time in local format:

out = Calendar.getinstance (). GetTime (). toLocaleString (); 2014-1-4 3:04:05

Gets the number of milliseconds for the current time of the system:

Long Currentstamp = Calendar.getinstance (). Gettimeinmillis (); 1388806099388
T.set (time);
Timestr = t.format2445 (); 20140104t112350

3. Other methods:

Gets the number of milliseconds that the non-sleep state has continued since the boot: (milliseconds of non-sleep uptime since boot.)

Long when = Systemclock.uptimemillis ();

It is more appropriate to detect a time difference in the execution of an operation or method, such as:

LOG.V ("Test Running Time", "This method costs time:" + (Systemclock.uptimemillis ()-when) + "MS");

Gets the number of milliseconds of the current time of the system, with the Time.settonow () method.

Long Time=system.currenttimemillis (); 1388803748818

T.set (time);
Timestr = t.format2445 (); 20140104t104908

Android platform related classes and methods for time and date (Date/time)

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.