I have created a date object and formatted it as follows :
Date dt = new java.util.Date();
SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
String currentTime = sdf.format(dt);
This formatted date will be used to update a field as date time in the database as follows.
String sql = "update userdetails set lastupdatedOn = '"+currentTime+"' where name ='userA'";
getJdbcTemplate().execute(sql);
The issue I am facing is that the date is always a couple of minutes behind the system time.
Edit : The code I have written here is just a sample to demonstrate what I wanted to accomplish. I cannot use CURRENT_TIMESTAMP or getDate() since it would fetch the time from DB sever system. I should be setting the time using java code instead. Though the time fetched by that manner is a couple of minutes behind the system time(the system in which the tomcat server is running) which is the issue I am facing.
Do not use string concatenation and to construct Sql which can lead to Sql Injection. Use prepared statement.
Do as below
String sql = "update userdetails set lastupdatedOn = ? where name ='userA'";
getJdbcTemplate().update(sql,new Object[]{dt})
OTOH
If you just want to insert current timestamp use CURRENT_TIMESTAMP
.
String sql = "update userdetails set lastupdatedOn = CURRENT_TIMESTAMP where name ='userA'";
getJdbcTemplate().execute(sql)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.