您的位置:首页 > 编程语言 > Java开发

Java的日期与时间(十)java.time.Instant

2017-04-03 23:27 239 查看

Java的日期与时间

原文链接

作者:Jakob Jenkov

译者:阿为

目录http://blog.csdn.net/tjgykhulj/article/details/68952451

所有译者备注将以此形式出现,删除线表示有争议或不明确的地方


Instant类在Java日期与时间功能中,表示了时间线上一个确切的点,定义为距离初始时间的时间差(初始时间为GMT 1970年1月1日00:00)

经测量一天有86400秒,从初始时间开始不断向前移动。

创建一个Instant实例

(原话:You create an Instant instance using one of the Instant

class factory methods. For instance, to create an Instant

which represents this exact moment of now, call

Instant.now(),我翻译不出这句的感觉,所以在此附上原文)

你可以通过Instant类的工厂方法创建一个Instant实例,例如你可以调用instant.now()来创建一个确切的表达当前时间的Instant对象:

Instant now = Instant.now();


另外也有一些其它方法能创建Instant,具体请查阅Java官方文档。

访问Instant的时间

一个Instant对象里有两个域:距离初始时间的秒钟数、在当前一秒内的第几纳秒,他们的组合表达了当前时间点。你可以通过以下两个方法得到它们的值:

long seconds =  getEpochSecond()
int nanos   =   getNano()


Instant的计算

Instant类有一些方法,可以用于获得另一Instant的值,例如:

plusSeconds()
plusMillis()
plusNanos()
minusSeconds()
minusMillis()
minusNanos()


我下面将向你展示两个例子,来说明这些方法如何使用:

Instant now     = Instant.now();
Instant later   = now.plusSeconds(3);
Instant earlier = now.minusSeconds(3);


第一行获得了一个Instant对象,表示当前时间。第二行创建了一个Instant表示三秒后,第三行创建了一个Instant表示三秒前。

下一章:java.time.Duration
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: