views:

71

answers:

2

Hi i am trying to parse a string into a java.sql.date

Here is what i am doing

private static SimpleDateFormat sdfout = new SimpleDateFormat("yyyy.MM.dd.HH.mm");
          try{
            String date = "2010.09.30.13.18";
        task.setDueDate(new java.sql.Date(sdfout.parse(date).getTime()));
    }

The problem is that i only get the date back. Not the time.

Am i doing this correctly

+1  A: 

The code logic is fine, you only need java.sql.Timestamp instead of java.sql.Date. The SQL timestamp represents both the date and time parts, like date in Java. The SQL date represents only the date part.

See also:


Noted should be that you should prefer java.util.Date over java.sql.Timestamp in the model objects and use the Timestamp only at the very moment when you're about to set it in a SQL query. This separates the model objects from the JDBC API and improves their portability and reusability.

BalusC
A: 

The java.sql.Date (according to JavaDoc)

A thin wrapper around a millisecond value that allows JDBC to identify this as an SQL DATE value.

It returns to you a representation of a SQL DATE.

The java.sql.Time (according to JavaDoc)

A thin wrapper around the java.util.Date class that allows the JDBC API to identify this as an SQL TIME value.

It returns to you a representation of a SQL TIME.

If you want both date and time, use the java.util.Date instead of java.sql.Date.:

The Elite Gentleman