• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java String comparison dilemma

 
Kaverappa Prabhakar
Ranch Hand
Posts: 43
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the code below, I was expecting System.out.println would not get executed but it does. If I switch the key and date position in CompareTo code then it won't execute.

Could any one explain to me why it is so?


 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both key and date are Strings. Did you check the API for the compareTo method in the java.lang.String class ? What happens when you compare "02009" to "12008", and "12008" to "02009" ?
 
Rob Spoor
Sheriff
Pie
Posts: 20396
47
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should consider using a (Simple)DateFormat to parse the strings into Date objects, then compare those.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic