简体   繁体   English

如何获取 java.sql.Time 表示的毫秒数?

[英]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):我有一个java.sql.Time (00:04:24) ,我想从中获取毫秒(或秒)数:

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

But the result are -3336000 instead of 264000 .但结果是-3336000而不是264000

Click here to see my output.单击此处查看我的输出。

Why isn't the output 264000 ?为什么不是输出264000

If you are using the modern JDBC version, you can use :如果您使用的是现代 JDBC 版本,则可以使用:

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 :或者按照@Andreas 的建议:

long millis = time.toNanoOfDay() / 1000000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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