简体   繁体   English

如何从datetimepicker接收日期并将其保存到数据库中?

[英]How to receive date from datetimepicker and save that into database?

I need to receive a date from user, and save that in database. 我需要从用户那里收到一个日期,并将其保存在数据库中。 I am using the following datetimepicker to receive the date, I receive the date as following, 我正在使用以下datetimepicker接收日期,收到以下日期,

It runs into following exception 它遇到以下异常

  SEVERE: org.hibernate.exception.GenericJDBCException: Value '0000-00-00' can not be represented 
  as java.sql.Date

Entered date 输入日期

   Thu Jun 13 00:00:00 EST 2012

How can I retrieve the date only to be able to save that into database? 我如何只检索日期才能将其保存到数据库中?

JSP JSP

  <sx:datetimepicker name="mydate"
                           label="myDate"
                           displayFormat="dd-MMM-yyyy"
                           value="todayDate"/>

Java 爪哇

...
private Date mydate;
...
@Temporal(javax.persistence.TemporalType.DATE)
getter

setter

Database 数据库

name     type
mydate   Date

Even when I use the following method as well to set the variable it throws the same exception 即使我也使用以下方法来设置变量,它也会引发相同的异常

public Date getCurrentDate(){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        System.out.println("Current Date" + dateFormat.format(date));
        return  date;
    }

Change display format dd-MMM-yyyy to MM-dd-yyyy 将显示格式dd-MMM-yyyy更改为MM-dd-yyyy

<sx:datetimepicker name="mydate"
    label="myDate"
    displayFormat="MM-dd-yyyy"
    value="todayDate"/>

Try this conversion 试试这个转换

java.sql.Date sqlDate = new java.sql.Date(struts2Date.getTime());

and save the sqlDate in mySQL table 并将sqlDate保存在mySQL表中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM