I have a starttime as textfield. I need to covert the String which I type in the text field to SQL date and store into database.
Default.xhtml
Start Date
<h:inputText id="startdate" value="#{customer.start_date}"
size="20" required="true"
label="startdate" >
</h:inputText>
CustomerBean.java
formatter = new SimpleDateFormat("dd/MM/yyyy");
Date startDate = (Date)formatter.parse(CustomerBean.this.getStart_date());
Date endDate = (Date)formatter.parse(CustomerBean.this.getEnd_date());
@SuppressWarnings("deprecation")
java.sql.Date startdate=new java.sql.Date(startDate.getDate());
@SuppressWarnings("deprecation")
java.sql.Date enddate=new java.sql.Date(endDate.getDate());
System.out.println("Today is " +date );
PreparedStatement ps
= con.prepareStatement(sql);
ps.setDate(1,startdate);
ps.setDate(2,enddate);
If I run this program I am getting the following error.
An Error Occurred:
java.text.ParseException: Unparseable date: "19/06/2012"
- Stack Trace
javax.faces.el.EvaluationException: java.text.ParseException: Unparseable date: "19/06/2012"
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
You need to use
java.util.Dateexclusively in the model class and usejava.sql.Dateexclusively in the DAO class. In the view you should use<f:convertDateTime>to convert between theStringrepresentation in HTML/HTTP and thejava.util.Datein the model class.Model:
View:
DAO: