STh
STh

Reputation: 812

How to get the number of milliseconds represented by a java.sql.Time?

I have a java.sql.Time (00:04:24) from which I want to get the number of milliseconds (or seconds):

ResultSet rs = ...;
java.sql.Time time = rs.getTime("DURATION");
long millis = time.getTime();

But the result are -3336000 instead of 264000.

Click here to see my output.

Why isn't the output 264000?

Upvotes: 2

Views: 860

Answers (1)

Youcef LAIDANI
Youcef LAIDANI

Reputation: 60046

If you are using the modern JDBC version, you can use :

LocalTime time = rs.getObject("DURATION", LocalTime.class);
long millis = time.toSecondOfDay() * 1000 + time.get(MILLI_OF_SECOND);

About the calculation of milliseconds, unfortunately, there are no method to get the Milliseconds of the day, for that I used that way.

Or as suggested by @Andreas :

long millis = time.toNanoOfDay() / 1000000

Upvotes: 2

Related Questions