简体   繁体   中英

Java: convert String to Date

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:

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) {
    // are there any exceptions thrown and caught here?
}

// are validStartDate and validEndDate non-null at this point? 
// is this code even executed? it won't be if an exception was thrown...
Assigment a  = new Assignment(..., validStartDate, validEndDate);

// where did this "s" come from? did you mean "session.save(a)"?
session.save(s);

Aside from these questions, you should check the following:

Is Hibernate really saving your Assignment in 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 your hibernate.cfg.xml file.

Only by checking all of above will you be able to solve your problem.

Is the Date object a java.util.Date object or a java.sql.Date? The reason I ask is that SimpleDateFormat parse will convert a String to a java.util.Date, but if you're using this object with an SQL database, I suspect you'll need the latter, the java.sql.Date. Fortunately they can be converted fairly easily. Also, what does your catch block say when you try to convert? Are there any exceptions being caught?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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