int compareTo(String str)
Here, str is the String being compared with the invoking String. The result of the comparison is returned and is interpreted as shown here:
Value Meaning
Less than zero The invoking string is less than str.
Greater than zero T he invoking string is greater than str.
Zero The two strings are equal.
If you want to ignore case differences when comparing two strings, use compareToIgnoreCase( ), shown here:
int compareToIgnoreCase(String str)
This method returns the same results as compareTo( ), except that case differences are ignored. This method was added by Java
2. You might want to try substituting it into the previous program. After doing so, "Now" will no longer be first.
More information: http://leepoint.net/notes-java/data/expressions/22compareobjects.html
Example :
/* Compare two Java Date objects using compareTo method example. This example shows how to compare two java Date objects using compareTo method of java Date Class. */ import java.util.Date; public class CompareDateUsingCompareToExample{ public static void main(String[] args) { //create first date object Date d1 = new Date(); //make interval of 10 millisecond before creating second date object try{ Thread.sleep(10); }catch(Exception e){ } //create second date object Date d2 = new Date(); System.out.println("First Date : " + d1); System.out.println("Second Date : " + d2); /* Use compareTo method of java Date class to compare two date objects. compareTo returns value grater than 0 if first date is after another date, returns value less than 0 if first date is before another date and returns 0 if both dates are equal. */ int results = d1.compareTo(d2); if(results > 0) System.out.println("First Date is after second"); else if (results < 0) System.out.println("First Date is before second"); else System.out.println("Both dates are equal"); } } /* TYPICAL Output Would be First Date : Sun Sep 09 19:50:32 EDT 2007 Second Date : Sun Sep 09 19:50:32 EDT 2007 First Date is before second */
No comments:
Post a Comment
I'm certainly not an expert, but I'll try my hardest to explain what I do know and research what I don't know.