Java - convert date formatted String from SQL db to date object for comparison -
Java - convert date formatted String from SQL db to date object for comparison -
i have string stored in database (example: sat jul 09 14:20:31 edt 2011
)
this stored string (i aware of sqls date capabilities, i'm not exploring @ point).
i want pull strong , compare current time using date.compare
function
date currentdate = new date(); // new current time value mcursor.getstring(mcursor.getcolumnindex(timecolumn)
//the old stored time value, pulled sql database
from database have string value formatted proper date. don't have obvious function compare current date date object.
i need know if 1 greater than, less than, or equal to, other. date compare
function allow this, object needs date object.
i have tried simpledateformat
seeded string's date format, parse exception "unparsable date", perhaps because formatted?
date currentdate = new date(); simpledateformat datetest = new simpledateformat("e m d hh:mm:ss z y"); if(currentdate.compareto(datetest.parse((mcursor.getstring(mcursor.getcolumnindex(timecolumn))))) > 0)
returns java.text.parseexception: unparseable date: sat jul 09 14:20:31 edt 2011
i suspect simpledateformat seed incorrect. insight appreciated
i believe need alter "m" "mmm" in format string:
simpledateformat format = new simpledateformat("e mmm d hh:mm:ss z y");
"m" potentially-single-digit value, e.g. 1, 10 etc. "mmm" short month name (e.g. "jul", "sep").
however, should also specify locale of date format. i'd strongly encourage break huge statement several bits create easier understand and debug:
int timecolumn = mcursor.getcolumnindex(timecolumn); string dbdatetext = mcursor.getstring(timecolumn); date dbdate = format.parse(dbdatetext); if (currentdate.compareto(dbdate) > 0) { }
i'd encourage utilize joda time date/time-related work, , avoid storing date/time info text in database :)
java sql date format simpledateformat
Comments
Post a Comment