tags:

views:

75

answers:

2

Hi, I am using SQLITE database and I store the date as a String. Now I want to compare both the string as a date. While I am using

String sqlQuery = "SELECT title,edate FROM lookup WHERE" + ((Date)df.parse("?")).getTime() + "<=" + date1.getTime(); c= db.rawQuery(sqlQuery,new String[]{"edate"});

it is giving error at run time. Please Tell me how can I compare two String as a Date.

Thank You Deepak

A: 
String formatString = "dd-MM-yyyy"; // for example
SimpleDateFormat df = new SimpleDateFormat(formatString);
Date date1 = df.parse(string1);
Date date2 = df.parse(string2);
if (date1.before(date2)) {
System.out.println("first date is earlier");
}
Roflcoptr
I am not able to create the object of SimpleDateFormat. In this line, I am getting a runtime error.
Deepak
Then it would be helpful if you post the stacktrace
Roflcoptr
I found the solution.....put the SimpleDateFormat in try catch block
Deepak
that isn't really the solution. Doing that you just don't see the exception any more.
Roflcoptr
+1  A: 

You want numbers in order to compare easily. I recommend POSIX (or unix) time, which counts seconds since a fixed point in about 1970.

Use an SQLite strftime function to convert your string to POSIX epoch time (check out the %s option) http://sqlite.org/lang_datefunc.html

Then use the Java functions to get epoch time: http://download.oracle.com/javase/6/docs/api/java/util/Date.html#getTime()

"SELECT title, edate FROM lookup WHERE strftime(edate,'%s') < " + (Date1.getTime()/1000)
Emyr
You should be using ISO8601 format for your date strings, read that sqlite page for info.
Emyr