I’m getting two dates as String values and I wanted to check start time is earlier than the end time. I compare them as it is without converting them to date using SimpleDateFormat, like this:
String startTime = "2013-01-02 14:25:56";
String endTime = "2013-01-02 14:30:56";
if(endTime.compareTo(startTime) > 0){
System.out.println("End time is greater than start time");
}
Is it really necessary to convert them to date and compare? Will I miss anything? Am I doing the right thing?
If you don’t have to include timezones and can ensure that you always have this format the lexical order will work.
You lose the flexibility
That depends on the point of view. I use something similar in a specialized search-enginge (only for performance reasons). Usually I convert to Date and compare these objects.