how can i compare a date with current date. I have to check the joining date of a student which is received from front end as a string.The joining date should not be greater than today's date.
I have tried the following. Its working but i think there must be a better way out
That looks a lot more complicated than it needs to be...
You can use class java.text.SimpleDateFormat to parse a string into a Date object. Then you can use the methods after() or before() of class Date to check if the date is before or after another date.
DateFormat.parse(String, ParsePosition) returns null when the string is invalid. That can be used:
Not only does this code not throw an exception, it also doesn't create an unnecessary Calendar (and Date from getTime()) object. You can improve this code slightly by checking if the entire String is used in parsing:
If the parse position's index is not directly at the end of the String there is something else in the String as well. This will occur if the passed String is something like "20/05/2010abc". The added check will make the method return false if that's the case.