I already read date functions but i cant think a best way to solve my problem.
I have a couple of dates from database which is String
and i want to compare it to may current date. I am using the compareTo
, but there is a problem using this function i guess it is because of i was comparing strings.
This is my function:
public int dateCompare(String today, String date2){
return today.compareTo(date2);
}
And when i use it in sample dates:
dateCompare("04/19/2013","04/18/2013");
it returns 1, and when i change the value of first parameter to "04/20/2013"
it still returns 1.
Please HELP...
This is the quikest i have used....
Java has a
Date
object. You should be using that instead.You could also just make use of the
Date
API and usebefore
andafter
...Fault is in the approach,
You are considering this as dateComparison, but look at the method arguments, String, String.
So you are actually comparing two string.
You should firstly be converting those Strings to date...
Like,
EDIT :
Modified for multiple formats: