Android development: Get the system time in the 12/24-hour format, android12

Source: Internet
Author: User

Android development: Get the system time in the 12/24-hour format, android12



// Obtain the system time String currentTime = DateFormat through DateFormat. format ("yyyy-MM-dd hh-mm-ss", new Date ()). toString (); currentTime = "time obtained through DateFormat: \ n" + currentTime; // obtain the 24-hour time SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH-mm-ss", Locale through SimpleDateFormat. getDefault (); currentTime + = "\ n" + "Get the 24-hour time through SimpleDateFormat: \ n" + sdf. format (new Date (); // obtain the 12-hour time sdf = new SimpleDateFormat ("yyyy-MM-dd hh-mm-ss", Locale. getDefault (); currentTime + = "\ n" + "use SimpleDateFormat to obtain the 12-hour time: \ n" + sdf. format (new Date ());

Note: Only the 12-hour time can be obtained through DateFormat.



How does android determine whether the system time is day or night?

You need to obtain the processing time during the day or night. However, the system time is in the 12-hour format, and does not show the day or night. Even if I change my cell phone to the 24-hour format, the time is also in the 12-hour format. It is similar that it cannot be divided into seven o'clock in the daytime or seven o'clock in the evening. Another problem is that in case the user uses the 12-hour system, I cannot judge it. Can you help me determine whether it is daytime or evening. Thank you. ------ Solution else this is difficult to judge through the client. The client time can be changed at Will ------ solution -------------------------------------------------------- yes, the AM and PM values in the 12-hour format represent the ContentResolver cv = getBaseContext () in the morning and afternoon respectively (). getContentResolver (); String strTimeFormat = android. provider. settings. system. getString (cv, android. provider. settings. system. TIME_12_24); if (strTimeFormat! = Null & strTimeFormat. equals ("24") {Log. I ("Debug", "24-hour system");} else {String amPmValues; Calendar c = Calendar ar. getInstance (); you do not need to care whether the user's time is 12 hours or 24 hours, as long as the time is correct,

Android obtains the system time

The Time obtained is in the 24-hour format. It is difficult to use it on your mobile phone. It may be because the Time format you set on your mobile phone is not in the 24-hour format!

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.