Parsing acquisition of system date and time in android

Source: Internet
Author: User
Tags add time

Copy codeThe Code is as follows: import java. text. SimpleDateFormat;

SimpleDateFormat formatter = new SimpleDateFormat ("MM dd, yyyy HH: mm: ss ");
Date curDate = new Date (System. currentTimeMillis (); // obtain the current time
String str = formatter. format (curDate );

You can obtain the current date and month, or write separately:Copy codeThe Code is as follows: SimpleDateFormat sDateFormat = new SimpleDateFormat ("yyyy-MM-dd hh: mm: ss ");
String date = sDateFormat. format (new java. util. Date ());

If you want to obtain the current year and month, you can write it like this (only get the same time or second ):
Java codeCopy codeThe Code is as follows: SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM ");
String date = sdf. format (new java. util. Date ());

Of course, you can also specify the time zone (to be ):Copy codeThe Code is as follows: df = DateFormat. getDateTimeInstance (DateFormat. FULL, DateFormat. FULL, Locale. CHINA );
System. out. println (df. format (new Date ()));

How to obtain whether the Android system is in the 24-hour or 12-hour format
Copy codeThe Code is as follows: ContentResolver cv = this. getContentResolver ();
String strTimeFormat = android. provider. Settings. System. getString (cv,
Android. provider. Settings. System. TIME_12_24 );

If (strTimeFormat. equals ("24 "))
{
Log. I ("activity", "24 ");
}

Copy codeThe Code is as follows: Calendar c = Calendar. getInstance ();
Obtain the system date: year = c. get (Calendar. YEAR)
Month = c. grt (Calendar. MONTH)
Day = c. get (Calendar. DAY_OF_MONTH)
Obtain the system time: hour = c. get (Calendar. HOUR_OF_DAY );
Minute = c. get (Calendar. MINUTE)

Obtain from Calendar
Copy codeThe Code is as follows: Calendar c = Calendar. getInstance ();
Obtain the system date: year = c. get (Calendar. YEAR)
Month = c. grt (Calendar. MONTH)
Day = c. get (Calendar. DAY_OF_MONTH)
Obtain the system time: hour = c. get (Calendar. HOUR_OF_DAY );
Minute = c. get (Calendar. MINUTE)
Calendar c = Calendar. getInstance ();
Obtain the system date: year = c. get (Calendar. YEAR)
Month = c. grt (Calendar. MONTH)
Day = c. get (Calendar. DAY_OF_MONTH)
Obtain the system time: hour = c. get (Calendar. HOUR_OF_DAY );
Minute = c. get (Calendar. MINUTE)

Obtain with Time
Copy codeThe Code is as follows: Time t = new Time (); // or Time t = new Time ("GMT + 8"); add Time Zone information.
T. setToNow (); // obtain the system time.
Int year = t. year;
Int month = t. month;
Int date = t. monthDay;
Int hour = t. hour; // 0-23
Int minute = t. minute;
Int second = t. second;

The only disadvantage is that the retrieval time is only in 24-hour mode.

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.