如何将 getTime 转换为秒?
您能帮忙解决一下问题吗:
我定义了一个变量,它是:
Time from_time = rs.getTime("nfrm_time");
它将读取值 7:15:00
如何将此类型转换为秒?
Can you please help in matter:
I have defined a variable which is:
Time from_time = rs.getTime("nfrm_time");
and it will read the values 7:15:00
How to convert this type to seconds?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
调用
getTime
获取自 1970 年 1 月 1 日以来的毫秒数。除以 1000 即可获取秒数:要获取当前日期 00:00 以来的秒数,请使用
Call
getTime
to get the number of milliseconds since January 1, 1970. Divide by 1000 to get it in seconds:To get the number of seconds since 00:00 of the current day, use the
long Seconds = rs.getTime("nfrm_time").getTime() / 1000
解释如下:
rs.getTime("nfrm_time")
返回java.sql .Time 实际上是 java.util.Date 的子类。
java.util.Date.getTime()
返回以毫秒为单位的时间,我们将其除以1000
以获得秒数。注意:
如果您要查找持续时间,
long seconds = rs.getTime("nfrm_time").getTime() / 1000
Here's the explanation:
rs.getTime("nfrm_time")
returnsjava.sql.Time
which is actually a sub class ofjava.util.Date
.java.util.Date.getTime()
returns time in milli seconds which we divide by1000
to get seconds.Note:
If you're looking for duration instead,
尝试:
这可能会起作用,因为:
这意味着日期部分始终是纪元日,这意味着
Time
实例由自一天开始以来的毫秒数表示。Try:
This might work since:
This means that the date part is always the epoch day, which means the
Time
instance is represented by the number of milliseconds since the beginning of the day.java.sql.Time 继承自 java.util.Date,它有一个 getTime() 方法,该方法返回自 1970 年 1 月 1 日 00:00:00 GMT 以来的毫秒数。
所以
from_time.getTime()/1000
应该可以解决问题。java.sql.Time inherits from java.util.Date which has a method getTime() which returns the number of milliseconds since January 1, 1970, 00:00:00 GMT.
So
from_time.getTime()/1000
should do the trick.更清洁的方法是
时间 from_time = Math.round(rs.getTime("nfrm_time")/1000);
Cleaner way is
Time from_time = Math.round(rs.getTime("nfrm_time")/1000);
要获取毫秒:
To get the milliseconds: