I was just wondering is there any way to compare to dates in Java, here is the situation i have two dates 26-11-1953 and 26-04-1981 both are in String format . Is there any way to compare them in java .
Change this string format into java.util.Date Object and then try the method compareTo
public int compareTo(Date anotherDate)Compares two Dates for ordering.
Parameters: anotherDate - the Date to be compared. Returns: the value 0 if the argument Date is equal to this Date; a value less than 0 if this Date is before the Date argument; and a value greater than 0 if this Date is after the Date argument.
[Satou]: If you want to know which one is before/after, you can still use String.compareTo.
If you do that, then "26-11-1953" is after "26-04-1981". Because "11" is after "04". The String class doesn't know anything about dates, and so alphabetic comparison is useless here. (Unless the dates had appeared in a sensible format like 1953-11-26 or 1951126, which are easily sortable as strings. But in general normal humans don't do that sort of thing, unfortunately.) To compare these strings correctly, you want to convert them to java.util.Date instances. There are several ways to do this - java.util.Calendar offers some particularly painful ways - but the easiest way is to use java.text.SimpleDateFormat: