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):此方法將添加瞬時經過的許多毫秒並傳回新的瞬時。
- 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:
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:
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:
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:
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:
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中文網其他相關文章!

本文討論了使用Maven和Gradle進行Java項目管理,構建自動化和依賴性解決方案,以比較其方法和優化策略。

本文使用Maven和Gradle之類的工具討論了具有適當的版本控制和依賴關係管理的自定義Java庫(JAR文件)的創建和使用。

本文討論了使用咖啡因和Guava緩存在Java中實施多層緩存以提高應用程序性能。它涵蓋設置,集成和績效優勢,以及配置和驅逐政策管理最佳PRA

本文討論了使用JPA進行對象相關映射,並具有高級功能,例如緩存和懶惰加載。它涵蓋了設置,實體映射和優化性能的最佳實踐,同時突出潛在的陷阱。[159個字符]

Java的類上載涉及使用帶有引導,擴展程序和應用程序類負載器的分層系統加載,鏈接和初始化類。父代授權模型確保首先加載核心類別,從而影響自定義類LOA


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

記事本++7.3.1
好用且免費的程式碼編輯器

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具