简体   繁体   中英

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 ?

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM