Deepak
Deepak

Reputation: 473

Comparing the Date in Android

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

Upvotes: 2

Views: 1150

Answers (2)

Emyr
Emyr

Reputation: 2371

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)

Upvotes: 2

anon
anon

Reputation:

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");
}

Upvotes: 1

Related Questions