4

How do I convert a Java TimeStamp object to a SQL datetime type that includes milliseconds? When I insert my TimeStamp obj into the database the milliseconds are gone.

This is my Java source code to translate a String into a Timestamp object:

import java.sql.Timestamp;
...
...
String timeStr = "2013-08-30 19:11:49.113";
Timestamp timeCreated = Timestamp.valueOf(timeStr);
timeCreated.toString(); //returns "2013-08-30 19:11:49.113"

My table dfinition in SQL:

CREATE TABLE `Event` (EventTimeStamp datetime NOT NULL);

This is how I insert the TimeStamp object into my Database:

PreparedStatement psInsert;
Connection conn = ...
psInsert = conn
            .prepareStatement("INSERT INTO `Event` (EventTimeStamp) VALUES(?)");  

psInsert.setTimestamp(1, timeCreated);
psInsert.executeUpdate();

After insertion the TimeStamp is cut and there are no milliseconds anymore:

2013-08-30 19:11:49

Thx

8
  • how do you check value in database? Commented May 16, 2016 at 16:14
  • In the MySql Workbench: SELECT * FROM Event; Commented May 16, 2016 at 16:17
  • what version of mysql? Commented May 16, 2016 at 16:18
  • fractional parts were added in 5.6.4 Commented May 16, 2016 at 16:20
  • Select Version(); says: 5.5.49 Commented May 16, 2016 at 16:23

1 Answer 1

2

Your database column datetime has no place for milliseconds. You should use datetime(3) instead.

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

1 Comment

This did the trick! Thx! But I needed to install the latest mysql version for this solution. @Lashane thx for the advice to update my mysql.

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.