8

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

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.