Android time and date Processing DateUtils, SystemClock, and DateFormat

Source: Internet
Author: User

Android time and date Processing DateUtils, SystemClock, and DateFormat

It always takes some processing of time and date, such as the time interval of a program and the formatting of date and time... note

 

Common java time and date class:

Java. util. Date

Java. util. Calendar

Java. text. DateFormat

Java. text. SimpleDateFormat

There is also a way to get the System time: System. currentTimeMillis ();

 

In android:

Android. OS. SystemClock
Android. text. format. DateFormat
Android. text. format. DateUtils

 

Android. OS. SystemClock

SystemClock. elapsedRealtime (); // the time after which the mobile phone starts (millisecond value, including the deep sleep time of the mobile phone)

The so-called android sleep, that is, after the screen is closed, no operation is performed for a period of time, no usb is connected, and then after a certain period of time,
Many services and processes on mobile phones are sleeping and no longer running.

SystemClock. uptimeMillis (); // how long has the phone been started (millisecond value, excluding the deep sleep time of the phone)
SystemClock. currentThreadTimeMillis (); // how long the current thread has been running (millisecond value, excluding the value of thread or systemclock. sleep)

 

Android. text. format. DateFormat

DateFormat. format ("yyyy-MM-dd HH: mm: ss", System. currentTimeMillis ());

DateFormat. format ("yy/MM/dd", Calendar. getInstance ());
DateFormat. format ("yyyy", new Date (2048,12, 01 ));

Based on the format and time (the format is defined in the class)

 

Android. text. format. DateUtils

DateUtils. formatDateTime (getApplicationContext (), // The formatting time, which can be up to minutes. Finally, set the display format for the parameter.
System. currentTimeMillis (),
DateUtils. FORMAT_24HOUR | DateUtils. FORMAT_SHOW_DATE | DateUtils. FORMAT_SHOW_TIME
| DateUtils. FORMAT_SHOW_YEAR | DateUtils. LENGTH_LONG | DateUtils. FORMAT_ABBREV_MONTH );

DateUtils. getRelativeTimeSpanString (System. currentTimeMillis () + 60*4000 ));

// Returns the string (minutes, hours, days, weeks, months, and years) represented by the maximum interval relative to the current time.

DateUtils. getRelativeTimeSpanString (context, long timeMillis );

// Returns the time string of the parameter relative to the current time: display time on the same day; display month and day on different days; display year, month, and day on different years

DateUtils. formatDateRange (getApplicationContext (), System. currentTimeMillis (), System. currentTimeMillis () + 60x60*3000,
DateUtils. FORMAT_SHOW_TIME); // returns the string between two time values.

 

 

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.