Java string operations and date operations, java strings
I. String operations
Create a string
String s2 = new String ("Hello World ");
String s1 = "Hello World ";
1. String connection
When multiple strings are connected, each string is connected with +, which is a string link. After the connection, a new string is generated.
2. Get the string length a. lenght ()
The string length a. substring () is truncated starting from 0 according to the index, and from the first digit to the third digit.
3. Obtain the indexOf () method for the index location of the specified string; lastIndexOf () method.
IndexOf (s) Return Value: Index of s that appears for the first time in the returned value string
Return Value of lastIndexOf (s): returns the index of s in the string
4. trim the leading and trailing spaces of the string trim ()
5. replace () All string replace () methods that match the specified string
6. Determine whether the string is equal to the equals () method
When you use equals () to compare strings, they are case sensitive. Under this condition, if the two strings still have the same characters and length, true is returned. If they are different, false is returned.
7. Determine the start of a string startsWith () method to determine the end of a string endsWith () method
This method is used to determine whether the current String object starts or ends with a specified parameter.
8. case-insensitive Conversion
Converts uppercase letters in a string to lowercase toLowerCase;
Converts lowercase letters in a string to uppercase toUpperCase.
9. String split (String sign) Method
This method completely separates strings Based on the specified delimiter.
Job:
Public static void main1 (String [] args) {String a = "substring"; for (int I = 0; I <4; I ++) {int B = (int) (Math. random () * 100) %. length (); String c =. substring (B, B + 1); System. out. print (. substring (B, B + 1) ;}// randomly generate a four-digit verification code that is not repeated
Public static void main (String [] args) {String a = "<student> <xm> Zhang San </xm> <xb> male </xb> </student> "; string B =. substring (. indexOf ("<xm>") + 4,. indexOf ("</xm>"); System. out. println ("name:" + B); String c =. substring (. indexOf ("<xb>") + 4,. indexOf ("</xb>"); System. out. println ("Gender:" + c);} // specifies the name and gender.
Ii. Date operation: Calendar
(1) read date
Calendar a = Calendar. getInstance ();
D. get (constant );
D. get (Calendar. YEAR); // returns the number YEAR.
D. get (Calendar. MONTH); // MONTH
D. get (Calendar. DAY_OF_MONTH); // day
D. get (Calendar. HOUR); // HOUR
D. get (Calendar. MINUTE); // MINUTE
D. get (Calendar. SECOND); // SECOND
D. get (Calendar. MILLISECOND); // MILLISECOND
Date Format display:
Format display SimpleDateFormat with date
1. Create the Calendar of the date
Calendar a = Calendar. getInstance ();
2. Create a formatter
SimpleDateFormat f = new SimpleDateFormat ("formatting style ");
Yy, yyyy -- year
M, MM -- month
D, dd -- day
H, hh -- hour, 12-hour; HH -- hour, 24-hour
M, mm-min
S, ss-seconds
3. format the calendar
F. format (Date); // note that Date is not a Calendar;
F. format (a. getTime (); // use the getTime () function of the Calendar Object to convert it to a Date object.
(2) write date
Calendar a = Calendar. getInstance ();
A. set (year, month, day );
A. set (year, month, day, hour, minute, second );
A. set (constant, value );
// C. set (, 12 );
// C. set (1999, 32 );
// C. set (Calendar. YEAR, 1980 );
Public class Dog {public static void main (String [] args) {Calendar a = Calendar ar. getInstance ();. set (2002, 2, 13); SimpleDateFormat B = new SimpleDateFormat ("MM dd, yyyy HH: mm: ss"); String c = B. format (. getTime (); System. out. println (c);} // format the date display
// The current time is not written in minutes and seconds