java Java中的即时时间戳
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38365130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Timestamp to Instant in Java
提问by user123475
I have a pojo which has a filed type as Instant. I want to set the Instant getting it from TimsStamp. Would it be possible?
我有一个归档类型为 Instant 的 pojo。我想设置从 TimsStamp 获取它的 Instant。这有没有可能?
For example: I have a java.sql.Timestamp
which I want to convert to java.time.Instant.
Would it be possible?
例如:我有一个java.sql.Timestamp
我想转换为java.time.Instant.
它可能吗?
回答by Dipak Prajapati
we have ready have existing methods which covert Timestamp to Instant and vice versa.
我们已经准备好了将 Timestamp 转换为 Instant 的现有方法,反之亦然。
Instant instant = Instant.now(); // get The current time in instant object
Timestamp t=java.sql.Timestamp.from(instant); // Convert instant to Timestamp
Instant anotherInstant=t.toInstant(); // Convert Timestamp to Instant
回答by Basit Anwer
Check this
检查这个
Converts this Timestamp object to an Instant. The conversion creates an Instant that represents the same point on the time-line as this Timestamp.
将此 Timestamp 对象转换为 Instant。转换会创建一个 Instant,它表示时间线上与此时间戳相同的点。