ozzboy
ozzboy

Reputation: 2720

Saving Timestamp data but only with date in Java

One of my database column is of type Timestamp format (yyyy-MM-dd HH:mm:ss) but the data saved in it is 2014-06-13 00:00:00. So the time component is not saved to the database table.

I am trying to INSERT data to that table but unable to remove the time component from my data.

Here is what I am doing:

long time = System.currentTimeMillis();
java.sql.Timestamp timestamp = new java.sql.Timestamp(time);

So the time (long) = 1402710418003 and timestamp=2014-06-13 21:46:58.003

Is there a way I can remove the time part from timestamp? Ex: 2014-06-13 OR Ex: 2014-06-13 00:00:00

This is my prepared statement:

java.util.Date currentDate= new java.util.Date();
sql_statement4.setTimestamp(6,new Timestamp(currentDate.getTime()));

sql_statement4 is a PreparedStatement object.

I want the value to be in Timestamp format.

How do I do this?

Upvotes: 1

Views: 2659

Answers (1)

Elliott Frisch
Elliott Frisch

Reputation: 201447

You need to change the call (from your comment),

sql_statement4.setTimestamp(6,new Timestamp(currentDate.getTime()));

to

sql_statement4.setDate(6,new java.sql.Date(currentDate.getTime()));

Upvotes: 2

Related Questions