Parsing string to java.sql.date -
i hope can me. input 22-01-1993
my code is
public string createcalendar (string titel, string startdate, string enddate, string note, string location, string email) throws sqlexception, parseexception { string stringtobereturned =""; testconnection(); simpledateformat format = new simpledateformat ("eee mmm dd hh:mm:s zzz yyyy"); java.util.date utilstartdate = format.parse(startdate); java.util.date utilenddate = format.parse(startdate); java.sql.date sqlstartdate = new java.sql.date(utilstartdate.gettime()); java.sql.date sqlenddate = new java.sql.date(utilenddate.gettime()); string [] keys = {"title","start","end", "note", "location", "active", "email"}; string [] values = {titel, format.parse(startdate).tostring(), format.parse(enddate).tostring(), note, location, "1", email}; qb.insertinto("calendar", keys).values(values).execute(); return stringtobereturned; }
however error java.text.parseexception: unparseable date: "22-01-1993" doing wrong
format.parse(startdate).tostring()
, format.parse(enddate)
trick you.
to add second question.
22-01-1993
how give data.
fri jan 22 00:00:00 cet 1993
how expects it.
so let's change ("dd-mm-yyyy")
match it.
("eee mmm dd hh:mm:s zzz yyyy");
that should trick.
apperently not same in php java, updated data (...)
match that. information taken
http://docs.oracle.com/javase/7/docs/api/java/text/simpledateformat.html
simpledateformat format = new simpledateformat("eee mmm dd hh:mm:s zzz yyyy"); string startdate = new date().tostring();
outputs
fri dec 05 23:15:39 cet 2014
so works.
the way see don't need following lines
java.util.date utilstartdate = format.parse(startdate); java.util.date utilenddate = format.parse(startdate); java.sql.date sqlstartdate = new java.sql.date(utilstartdate.gettime()); java.sql.date sqlenddate = new java.sql.date(utilenddate.gettime());
a problem when having the following input 22-01-1993
doesn't match expected have. extract month, day , year it, build new string rest of data. however, feels redundant.
Comments
Post a Comment