1

I have created a table as below:

create table sample(id int not null,created_at timestamp,updated_at timestamp,name varchar(100),primary key(id));

I have tried to update the column in different ways as below, but am not able to update the time:

Calendar calender = Calendar.getInstance();
java.util.Date now = calender.getTime();
java.sql.Timestamp updatedAt = new java.sql.Timestamp(now.getTime());
stmt.setTimestamp(2, updatedAt);

java.sql.Timestamp  updatedAt = new java.sql.Timestamp(new java.util.Date().getTime());
stmt.setTimestamp(2, updatedAt);
    
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
String updatedAt =sdf.format(date);
stmt.setString(2, updatedAt);

final java.util.Date updatedAt = new java.util.Date(System.currentTimeMillis());
stmt.setDate(2, new java.sql.Date(updatedAt.getTime()));

Please, help me to resolve this issue?

4
  • Please tell us, what is happening? Commented Jul 30, 2014 at 7:08
  • unable to update the time in database table , it is updating like 2014-07-29 00:00:00 Commented Jul 30, 2014 at 7:09
  • your database has a wrong type? Commented Jul 30, 2014 at 7:11
  • No i have not written any trigger @Scary Wombat Commented Jul 30, 2014 at 7:15

1 Answer 1

2

The java.sql.Date() strips time portion of your date due to SQL convention. You can check SQL-92 standard.

Timestamp updatedAt = new Timestamp(Calendar.getInstance().getTimeInMillis());
stmt.setTimestamp(2, updatedAt);
Sign up to request clarification or add additional context in comments.

10 Comments

according to his code java.sql.Timestamp updatedAt = new java.sql.Timestamp(now.getTime()); stmt.setTimestamp(2, updatedAt); he already does
also according to javadoc sql.Date is A thin wrapper around a millisecond value that allows JDBC to identify this as an SQL DATE value
@ScaryWombat His code is wrong, and it didn't used milliseconds to create a timestamp.
Yes @Scary Wombat have already tried the above code but as date is having milliseconds we are unable to update the column
@RomanC java.util.Date now Date.getime () returns millseconds
|

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.