Marco Dinatsoli
Marco Dinatsoli

Reputation: 10590

datetime in sql changes to just date in my application

I have a column in my table from type datetime, I am retrieve that column to my java application using jdbc.

my problem is that when I stored 2014-05-13 23:49:18.150 in my database, it becomes 2014-04-13 in my java application.

how to get all the date include time (house, seconds , minutes ).

java code

callableStatement = con
                    .prepareCall("{call getStatusForOrder(?,?,?,?,?)}");
            callableStatement.setInt(1, order.getID());
            callableStatement.registerOutParameter(2, java.sql.Types.INTEGER);
            callableStatement.registerOutParameter(3, java.sql.Types.DATE);
            callableStatement.registerOutParameter(4, java.sql.Types.DATE);
            callableStatement.registerOutParameter(5, java.sql.Types.BIT);
            callableStatement.execute();
            int statusID = callableStatement.getInt(2);
            Date startTime = callableStatement.getDate(3);
            Date endTime = callableStatement.getDate(4);
            boolean isActive = callableStatement.getBoolean(5);
            System.out.println("IsActive = " + isActive);
            System.out.println("Start Date = " + startTime.toString());

Upvotes: 2

Views: 1787

Answers (4)

CloudyMarble
CloudyMarble

Reputation: 37576

Use the Timestamp instead of Date, both datetime and datetime2 map to java.sql.Timestamp.

See the documentation.

PS. Do not forget to use callableStatement.getTimestamp instead of callableStatement.getDate.

http://docs.oracle.com/javase/6/docs/api/java/sql/CallableStatement.html#getTimestamp(int)

Upvotes: 4

Mark Rotteveel
Mark Rotteveel

Reputation: 109261

Types.DATE signifies a JDBC (and SQL) datatype that only has a date component, and no time. You need to use Types.TIMESTAMP and the associated get/setTimestamp(..) methods and class java.sql.Timestamp if you want to have the time portion included.

Upvotes: 1

max_
max_

Reputation: 324

I think you must use Timestamp (if I recall it right, haven't programmed Java for a while) instead of Date

Upvotes: 1

Andrew Thompson
Andrew Thompson

Reputation: 168845

Use a DateFormat to display it.

  • SHORT is completely numeric, such as 12.13.52 or 3:30pm
  • MEDIUM is longer, such as Jan 12, 1952
  • LONG is longer, such as January 12, 1952 or 3:30:32pm
  • FULL is pretty completely specified, such as Tuesday, April 12, 1952 AD or 3:30:42pm PST.

Upvotes: 2

Related Questions