How to compare the size of two time strings and the size of two strings
Method 1: backend
There are now two strings that are date-based reading from the form, "2003-10-12" and "2003-10-21 ",
How can we determine their size.
If you do not want to convert it to date type comparison
There is a simple method
String a = "2003-10-12 ";
String B = "2003-10-21 ";
Int result = a. compareTo (B );
If (result = 0)
Out. print ("a = B ");
Else if (result <0)
Out. print ("a <B ");
Else out. print ("a> B ");
Use the compareTo () method in the String class for comparison. For example, s1.compareTo (s2)
However, this method has the disadvantage that the form and length of the two strings involved in the comparison must be the same. Otherwise, the comparison result is wrong.
For example:
S1 = "02 ";
S2 = "12 ";
This is correct.
But if it looks like the following,
S1 = "2 ";
S2 = "12 ";
The comparison result is incorrect.