I’m using a Java method to convert the date into suitable format:
private Timestamp toTimeStamp(String s) throws java.text.ParseException
{
Timestamp ts = null;
java.util.Date date = null;
if (s == null || s.trim().isEmpty()) return ts;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.S");
date = (java.util.Date) sdf.parse(s);
ts = new Timestamp(date.getTime());
return ts;
}
When I tried to execute the form I get this error:
Unparseable date: "30-Jun-12"
I tried to change the the date format but I still get this error. Can you help me to fix it?
There are 2 problems:
yyyy-MM-dd hh:mm:ss.Scan never match30-Jun-12. You needdd-MMM-yy. See alsoSimpleDateFormatjavadoc.So this should do:
A third problem, which is actually more a design matter, you should not be using
java.sql.Timestamp(and other SQL specific date types) anywhere in your model/business layer (you originally tagged the question JSF). You should only use it in your data layer. Usejava.util.Dateinstead and convert only at exactly the moment you need to persist it in the DB like so