简体   繁体   English

如何从java.sql.Timestamp中检索毫秒数

[英]How to retrieve milliseconds from java.sql.Timestamp

我有一个时间戳对象,需要从它获得毫秒,有人可以帮我一个示例代码片段吗?

You can use Timestamp.getTime() 你可以使用Timestamp.getTime()

Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Timestamp object. 返回自此Timestamp对象表示的1970年1月1日00:00:00 GMT以来的毫秒数。

Example: 例:

long timeInMilliSeconds = t.getTime();
// do magic trick here

Note: Timestamp is extend from Date . 注意: 时间戳Date延伸。

You can just call getTime() to get milliseconds since the Unix epoch. 你可以调用getTime()来获得自Unix时代以来的毫秒数。 Is that what you were after, or did you want "milliseconds within the second" or something similar? 这就是你所追求的,或者你想要“毫秒内的毫秒”或类似的东西?

Note that using just milliseconds is slightly odd for a Timestamp , given that it's designed specifically to be precise to the nanosecond. 请注意,对于Timestamp使用毫秒对于Timestamp有点奇怪,因为它专门设计为精确到纳秒。 So you should usually be using getTime() in conjunction with getNanos() . 所以你通常应该将getTime()getNanos()结合使用。

From the Java Docu ( link ): 来自Java Docu( 链接 ):

public long getTime() public long getTime()

Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Timestamp object. 返回自此Timestamp对象表示的1970年1月1日00:00:00 GMT以来的毫秒数。

I guess this will give the output you want. 我想这会给你想要的输出。

public void timeInMills(Timestamp t){
    System.out.println("Time in Milli second "+t.getTime());

}

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

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