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.