views:

49

answers:

2

I have a database with a field for a stored date. I would like to be able calculate the days between the recorded date and today.

I ended up using:

mDb.execSQL("UPDATE "+DATABASE_PLANTS_TABLE+" SET "+ KEY_PLANT_DAYS+ 
            " = (SELECT julianday('now') - julianday("+KEY_DATE+") FROM"+ DATABASE_PLANTS_TABLE+")");
A: 

Use the Joda Time Days class for this:

Date dateInDb = getStoredDate();
Date today = new Date();
Days days = Days.daysBetween(new DateTime(dateInDb), new DateTime(today));
int numberOfDays = days.getDays();
StudiousJoseph
I've downloaded Joda Time, not sure how to use it though. I've attempted to add the class directly to my project but get a source not found error when attempting to open it.
Brian
Been working on this currently the 'p' in p.getDays(); is unresolved
Brian
Sorry Brian, it was a typo, now its fixed!!! Thanks :)
StudiousJoseph
Thanks for the help so far. Now i'm getting a force close which seems to be an android issue. No errors in debug and it happens when calling DateTime. a simple: DateTime start = new DateTime(dateInDb); causes a force close
Brian
A: 
select (strftime('%s','now')-strftime('%s', data1))/86400.0 as days from table
Pentium10
i ended up using something very similar to this. Much easier than bothering with jodatime since all i needed was this date calculation.What i actually used is posted above.
Brian