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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 03:20:47  来源:igfitidea点击:

Timestamp to Instant in Java

javatimetimestampjava.time.instant

提问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.Timestampwhich 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

检查这个

public Instant toInstant()

public Instant toInstant()

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,它表示时间线上与此时间戳相同的点。