The Date class is used to specify date and time. Its constructor and common methods are as follows:
publicDate()
Construct a datetime object from the current time.
publicStringtoString()
Convert to a string.
publiclonggetTime()
Returns the number of milliseconds since the new century, which can be used for time calculations.
[Example 3.10] The time taken for the test execution cycle (the order of magnitude is milliseconds), the specific time situation is shown in Figure 3.9. The source program code is as follows:
//The program file name is UseDate.java import java.util.Date; public class UseDate { public static void main(String[] args) { Date dOld = new Date(); long lOld = dOld.getTime(); System.out.println("The system time before the loop is: " +dOld.toString()); int sum = 0; for (int i=0; i<100; i++) { sum += i; } Date dNew = new Date(); long lNew = dNew.getTime(); System.out.println("The system time after the loop is: " +dNew.toString()); System.out.println("The number of milliseconds spent on the loop is: " + (lNew - lOld)); } }The results show:
String class
The String class is used to operate non-numeric strings. It provides seven types of method operations, namely string creation, string length, string comparison, string retrieval, string interception, string operation and data type conversion.
2. String length
public int length()
Returns the length of the string.
3. String comparison
public boolean equals(Object anObject)
Compare whether the string is the same as the string represented by anObject (case sensitive).
public boolean equalsIgnoreCase(String anotherString)
Compare whether the string is the same as anotherString (case insensitive).
1. String creation
public String()
Construct an empty string.
public String(char[] value)
Use characters in the character array value to construct a string.
public String(String original)
Use a copy of the original string to construct a new string.
4. String Retrieval
public int indexOf(String str)
Returns the location where str appears for the first time in a string.
public int indexOf(String str, int fromIndex)
Returns where the string str appears fromIndex starts.
5. String interception
public String substring(int beginIndex, int endIndex)
Returns the string between benginIndex and endIndex.
6. String operation
The operator is "+", which indicates the connection operation. The following line statement outputs the connected string.
System.out.println("Hashtable:" + hScore.toString());
[Example 3.11] Operation string, the output result is shown in Figure 3.10. The source program code is as follows:
//The program file name is TestString.java public class TestString { public static void main(String[] args) { String str = new String("The substring begins at the specified beginIndex."); String str1 = new String("string"); String str2 = new String(); int size = str.length();//String length int flag = str.indexOf("substring"); str2 = str.substring(flag,flag + 9);//Fetch the substring System.out.println("String" + str + "/n The total length is: " + size); if(str1.equals(str2))//Judge whether it is equal System.out.println("The intercepted string is: " + str1); else System.out.println("The intercepted string is: " + str2); } }The results show:
Summarize
The above is all the content shared in this article about the example code of the java date class and string class. I hope it will be helpful to everyone. Interested friends can continue to refer to this site:
JAVA StringBuffer class and StringTokenizer class code analysis
A brief discussion on class classes in Java
Example of fully qualified name code for Java internal classes
If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!