0

我有一个作为文本字段的开始时间。我需要将我在文本字段中键入的字符串转换为 SQL 日期并存储到数据库中。

默认.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);

如果我运行这个程序,我会收到以下错误。

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)
4

1 回答 1

2

需要java.util.Date在模型类中java.sql.Date独占使用,在DAO类中独占使用。在视图中,您应该使用在 HTML/HTTP中的表示和模型类中<f:convertDateTime>的表示之间进行转换。Stringjava.util.Date

模型:

import java.util.Date;

// ...

private Date startDate;
private Date endDate;

看法:

<h:inputText value="#{customer.startDate}">
    <f:convertDateTime pattern="dd/MM/yyyy" />
</h:inputText>
<h:inputText value="#{customer.endDate}">
    <f:convertDateTime pattern="dd/MM/yyyy" />
</h:inputText>

道:

import java.sql.Date;

// ...

preparedStatement.setDate(1, new Date(customer.getStartDate().getTime()));
preparedStatement.setDate(2, new Date(customer.getEndDate().getTime()));
于 2012-06-18T12:57:02.100 回答