I need to convert a String like 2010-11-28 into Date type to be saved in a PostgreSQL database. I tried this code but when I check the result in a database both dates are null:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date validStartDate=null;
Date validEndDate=null;
try {
validStartDate = df.parse(startDate);
validEndDate = df.parse(endDate);
} catch (Exception e) {
...
}
Assigment a = new Assignment(..., validStartDate, validEndDate);
session.save(s);
——————————–Edit—————————-
startDate and endDate are Strings in a format yyyy-mm-dd like 2010-11-27. There is nothing in the stack trace (the conversion seems to somehow work). If there was something I would probably know what is wrong. After conversion the dates are stored in a postgres database. Yes, I used util.Date instead of sql.Date (probably that’s a first mistake).
——————————- Edit2—————————
The code is simplified but: the stack trace doesn’t show anything strange, validStartDate and validEndDate are converted to something like this Tue Nov 30 00:00:00 CET (so not null). The Assignment is indeed correctly saved in a database. The sql: insert into public.assignment (type, valid_start_date, valid_end_date, id) values (5, NULL, NULL, 2). I tried to use sql.Date but no success.
—————————-Assignment mapping—————–
<hibernate-mapping>
<class name="model.Assignment" schema="public" table="assignment">
<id name="id" type="int">
<column name="id"/>
<generator class="increment"/>
</id>
<property name="validStartDate" type="date">
<column name="valid_start_date" length="13" />
</property>
<property name="validEndDate" type="date">
<column name="valid_end_date" length="13" />
</property>
<property name="type" type="integer">
<column name="type" />
</property>
</class>
</hibernate-mapping>
I’m using Hibernate to save user data. The String is created by a JQuery widget Datepicker. Does anyone know how to solve that problem? Thanks in advance for help.
A few comments with questions:
Aside from these questions, you should check the following:
Is Hibernate really saving your
Assignmentin database?Are your Hibernate mappings correct?
Did you try using
java.sql.date?What is the generated SQL? You can turn that option on by using
<property name="show_sql">true</property>in yourhibernate.cfg.xmlfile.Only by checking all of above will you be able to solve your problem.