日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java Instant类

發布時間:2025/3/15 java 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java Instant类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

時間點

此類的對象表示時間線上的一點。可以理解為人類的絕對時間。

因為是時間線的一點,所以時間點可以比較大小,那么這個時間點的標準都是統一的(UTC時間);即這個時間點對于整個地球人們來說是唯一的,不是北京時間、也不是東京時間、而是世界時間。

與時間點相關的兩個概念就是時間線和時間的單位。

一、時間線

時間點在時間線上(點在線上)。時間線上有三個重要的點:最大值點、最小值點、原點(時間不知道有沒有盡頭)。
在Java世界,時間線的末端分別是Instant.MAX和Instant.MIN;時間線的原點是Instant.EPOCH

(1)時間線上的原點

System.out.println(Instant.EPOCH); 結果:1970-01-01T00:00:00Z

(2)時間線的最大值

System.out.println(Instant.MAX); 結果:+1000000000-12-31T23:59:59.999999999Z

(3)時間線的最小值

System.out.println(Instant.MIN); 結果:-1000000000-01-01T00:00:00Z

二、時間的單位

年、月、日、時、分、秒都是時間的單位。
現在人們把控自己生活節奏已經到了分鐘這個單位;而科技的進步要求把控我們的機器的時間單位是秒、毫秒、納秒。

在時間線上,我們使用秒(s)作為時間點的單位。

  • 1s(1秒)= 1000ms(1000毫秒) = 1000_000us(微妙)=1000_000_000ns(納秒)

  • 1day(天)=86400s(秒)=8640000ms(秒)

  • 1year(年)= 365.2425day(天) = 31556952s(秒)= 31556952000ms(毫秒)

在Instant對象中,有一個long長度的值表示距離原點的長度(單位秒),一個int長度的值表示納秒(永遠是正數,不會大于999_999_999;因為1000_000_000ns = 1s)。

源碼:

/*** The number of seconds from the epoch of 1970-01-01T00:00:00Z.*/ private final long seconds; /*** The number of nanoseconds, later along the time-line, from the seconds field.* This is always positive, and never exceeds 999,999,999.*/ private final int nanos;

三、時間點的構造

  • (1)當前時間點
Instant now = Instant.now();System.out.println(now);System.out.println(now.getEpochSecond());System.out.println(now.getNano());System.out.println(now.toEpochMilli());

結果:

2019-04-18T08:04:29.633Z 1555574669 633000000 1555574669633
  • (2)根據距離原點的秒構造時間點

這個構造方法不夠精準,無法到納秒級。

①距離原點1秒

Instant now = Instant.ofEpochSecond(1L);System.out.println(now);System.out.println(now.getEpochSecond());System.out.println(now.getNano());System.out.println(now.toEpochMilli());

結果:

1970-01-01T00:00:01Z 1 0 1000

②距離原點-1秒

Instant now = Instant.ofEpochSecond(-1L);System.out.println(now);System.out.println(now.getEpochSecond());System.out.println(now.getNano());System.out.println(now.toEpochMilli());

結果:

1969-12-31T23:59:59Z -1 0 -1000
  • (3)構造到納秒級的時間點

源碼:

public static Instant ofEpochSecond(long epochSecond, long nanoAdjustment) {long secs = Math.addExact(epochSecond, Math.floorDiv(nanoAdjustment, NANOS_PER_SECOND));int nos = (int)Math.floorMod(nanoAdjustment, NANOS_PER_SECOND);return create(secs, nos);}

這個方法有兩個參數,第一個是秒,第二個是可以為正負的納秒值。

Instant now = Instant.ofEpochSecond(5L,500L); System.out.println(now); System.out.println(now.getEpochSecond()); System.out.println(now.getNano()); System.out.println(now.toEpochMilli()); 1970-01-01T00:00:05.000000500Z 5 500 5000 Instant now = Instant.ofEpochSecond(5L,-500L);System.out.println(now);System.out.println(now.getEpochSecond());System.out.println(now.getNano());System.out.println(now.toEpochMilli()); 1970-01-01T00:00:04.999999500Z 4 999999500 4999
  • (4)構造到毫秒級的時間點
Instant now = Instant.ofEpochMilli(5500L);System.out.println(now);System.out.println(now.getEpochSecond());System.out.println(now.getNano());System.out.println(now.toEpochMilli()); 1970-01-01T00:00:05.500Z 5 500000000 5500
  • (5)根據文本構造時間點
    對字符串的格式有要求,字符串必須如同: 2007-12-03T10:15:30.00Z
Instant now = Instant.parse("1007-12-03T10:15:30.00Z");System.out.println(now);System.out.println(now.getEpochSecond());System.out.println(now.getNano());System.out.println(now.toEpochMilli()); 1007-12-03T10:15:30Z -30360318270 0 -30360318270000

四、時間點的比較

A.compareTo(B):A>B,return 1;A= B,return 0;A<B,return -1;

Instant datetime1 = Instant.parse("2007-12-03T10:15:30.00Z"); Instant datetime2 = Instant.parse("2017-12-03T10:15:30.00Z"); Instant datetime3 = Instant.parse("2019-12-03T10:15:30.00Z");System.out.println(datetime2.compareTo(datetime3)); System.out.println(datetime2.compareTo(datetime2)); System.out.println(datetime2.compareTo(datetime1)); System.out.println(datetime2.isAfter(datetime2)); System.out.println(datetime2.isBefore(datetime3)); -1 0 1 false true

五、時間點的運算

  • (1)時間點加秒級、毫秒、納秒
    ①原點加100000s
Instant plusSeconds = Instant.EPOCH.plusSeconds(100000L);System.out.println(plusSeconds);System.out.println(plusSeconds.getEpochSecond());System.out.println(plusSeconds.toEpochMilli());System.out.println(plusSeconds.getNano()); 1970-01-02T03:46:40Z 100000 100000000 0
  • (2)時間點加任意時間單位
    ①原點加一百天
Instant plus100days = Instant.EPOCH.plus(100, ChronoUnit.DAYS); System.out.println(plus100days); System.out.println(plus100days.getEpochSecond()); System.out.println(plus100days.toEpochMilli()); System.out.println(plus100days.getNano()); 1970-04-11T00:00:00Z 8640000 8640000000 0

文章轉自

總結

以上是生活随笔為你收集整理的Java Instant类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。