jayantS
jayantS

Reputation: 837

Retrieving MySQL time in java

I am running the following query to get time difference of two times

resultSet = statement.executeQuery("SELECT TIMEDIFF('14:03:55.256', '14:02:51.780') AS td");

MySQL gives time difference in this format

00:01:03.476

But both resultSet.getTime("td"); and resultSet.getObject("td");
returns 00:01:03

According to the documentation getTime(String string) retrieves the value of the designated column in the current row of this ResultSet object as a java.sql.Time object in the Java programming language.

java.sql.Time corresponds to SQL TIME and contains information about hour, minutes, seconds and milliseconds.Then why am I getting 00:01:03 instead of 00:01:03:476?

What I basically want is to store 00:01:03.476 into a String. Is there any workaround or I am doing it wrong?

Upvotes: 3

Views: 9267

Answers (2)

Paolo
Paolo

Reputation: 1691

Try to do this:

String s = new SimpleDateFormat("HH.mm.ss.SSS").format(t.getTime());

where t is your variable of type java.sql.Time.

Hope this will solve you problem.

Upvotes: 3

Java Devil
Java Devil

Reputation: 10959

If you are verifying the result by printing it out note that java.sql.Time.toString() only returns a string in the format hh:mm:ss

You should really use rs.getTimestamp which will return a java.sql.Timestamp

Upvotes: 4

Related Questions