首頁 > Java > java教程 > 主體

Java即時

WBOY
發布: 2024-08-30 15:49:36
原創
531 人瀏覽過

Java Instant 是 Java 中一組預先定義的方法,可以在程式碼區塊中使用它們來執行程式中的特定功能。一些最常用的Java Instant 方法是toString()、parse()、ofEpochMilli()、getEpochSecond()、getNano()、plusMillis(long milliToAdd)、plusNanos(long nanosToAdd)、plusSeconds(long SecondsToAdd)、minusSeconds (long SeconToAdd)、plusSeconds(long SecondsToAdd)、minusSeconds (long SeconusSeconds (long Second. )、minusMillis(long millisToSubtract)、minusNanos(long nanosToSubtract)、compareTo(Instant otherInstant)、isAfter(Instant otherInstant) 和isBefore(Instant otherInstant)。 Java 中的這項特性以其更高的效率、卓越的效能、可重複使用性、最佳化的程式碼長度等而聞名

文法

Instant類別提供了多個靜態工廠方法來建立Instant類別的實例。以下是我們可以取得不同標準值的 Instant 類別實例的靜態方法。

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

Instant instant = Instant.now();
登入後複製

now() 方法傳回系統時鐘的當前時刻。此實例表示從開始時間或 EPOCH 開始的奈秒總數。這是從 1970 年 1 月 1 日開始計算的時間。這種獲取瞬間的方法對於表示機器時間戳很有用,並且會比其他方法更頻繁地使用。

Instant instant = Instant.EPOCH;
登入後複製

此靜態欄位將傳回表示確切 EPOCH 時間的 Instant 類別的實例。

Instant instant = Instant.MAX;
登入後複製

這個靜態欄位將會傳回一個 Instant 類別的實例,表示該實例的最大值。

Instant instant = Instant.MIN;
登入後複製

這個靜態欄位會傳回一個Instant類別的Instance,代表instance的最小值,值為負數。

Java Instant 的方法

接下來就是Instant類別的主要部分或者說用法了。以下是 Instant 類別的一些主要方法的清單。

  • toString(): 此方法從 Object 類別重寫,以使用 ISO-8601 標準以字串格式表示實例。

以下是可用來取得 Instant 類別實例的方法。

  • parse(): 此方法採用文字字串作為參數,並傳回表示傳遞的相同值的 Instance 類別的實例。該字串應在 UTC 時間立即有效。
  • ofEpochMilli(): 此方法將輸入長(毫秒)作為參數,並傳回表示傳遞的相同值的 Instance 類別的實例。此方法可用於將 java.util.Date 物件轉換為 Instant.

Instant 類別的實例表示時間,與 util.Date(毫秒精度)相比,具有奈秒精度。因此,瞬間需要更多的儲存空間來儲存其值(大於長)。因此,即時類別將秒的值儲存在 long 變數中,並將剩餘的奈秒值儲存在 int 變數中。我們可以使用以下方法存取這些單獨的值。

  • getEpochSecond(): 此方法將只傳回 EPOCH 中的秒數。
  • getNano(): 此方法傳回從時間軸或秒開始算起的奈秒數。

以下是可用於即時操作或計算的方法。

  • plusMillis(long millisToAdd):此方法將添加瞬時經過的許多毫秒並傳回新的瞬時。
注意:請注意,Instant 類別是一個不可變類,因此它將為每個操作操作傳回一個新實例。
  • plusNanos(long nanosToAdd):與上一個類似,但增加了奈秒。
  • plusSeconds(long SecondsToAdd):秒的新增。

減法或減法運算也有類似的方法。這些方法將從該瞬間減去經過的秒數並返回一個新的瞬間。

  • 減秒(長秒減)
  • minusMillis(long millisToSubtract)
  • minusNanos(長 nanosToSubtract)

以下是可用來比較兩個時刻的方法,

  • compareTo(Instant otherInstant): This method will compare the one instant with the passed one. Returns the integer value negative if it is less and positive if it is greater.
  • isAfter(Instant otherInstant): This method checks if the passed instant is after the current instant. Returns true or false.
  • isBefore(Instant otherInstant): Similar to the previous one, checks if the passed instant is before the current instant. Returns true or false.

Examples to Implement Java Instant

Below are the examples of implementing java instant:

1. toString()

Code:

import java.time.Instant;
public class app {
public static void main(String[] args) {
Instant instant = Instant.now();
System.out.println( instant.toString() );
}
}
登入後複製

Output:

Java即時

The output will be the time of the system running the code.

2. getEpochSecond()

Code:

import java.time.Instant;
public class app {
public static void main(String[] args) {
Instant instant = Instant.now();
System.out.println( instant.getEpochSecond() );
}
}
登入後複製

Output:

Java即時

3. getNano()

Code:

import java.time.Instant;
public class app {
public static void main(String[] args) {
Instant instant = Instant.now();
System.out.println( instant.getNano() );
}
}
登入後複製

Output:

Java即時

4. plusSeconds()

Code:

import java.time.Instant;
public class app {
public static void main(String[] args) {
Instant instant = Instant.now();
System.out.println( instant );
instant = instant.plusSeconds( 3600 );
System.out.println( instant );
}
}
登入後複製

Output:

Java即時

We have added exactly 3600 seconds, i.e. 1 hour, to the current instant, as it can be seen in the output that time is increased by 1 hour.

5. compareTo()

Code:

import java.time.Instant;
public class app {
public static void main(String[] args) {
Instant instant = Instant.now();
System.out.println( instant );
Instant instant2 = instant.plusSeconds( 3600 );
System.out.println( instant.compareTo( instant2 ) );
}
}
登入後複製

Output:

Java即時

Here, we are comparing two instants, current with the instant having added 1 more hour. As the current instance is less than instance2, the output is negative.

Conclusion

So, we have seen the Instant class introduced from Java 8. This class represents the seconds from the start of the timeline with nanoseconds precision. This class is useful to generate the timestamp, which will represent the system time. We have seen different types of instants which we can get and different methods useful for calculations, creating a new instantly, comparing instants, getting seconds and nanoseconds differently, etc. Instant class is immutable and provides thread safety as compared to the old Date object. The Instant class is much more powerful than the old time-date API.

以上是Java即時的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!