Java LocalTime 是內建的功能包之一,可用於與日期和時間相關的多種操作。對於與 LocalTime 相關的操作,Java 有一個名為 Java Time 的特定應用程式介面 (API),它擁有多種方法可將其應用於任何與日期和時間相關的函數。 LocalTime 類別中常用的一些方法有get()、compareTo()、equals、atDate()、of()、now()、plusHours()、minusHours() 等,以小時、分鐘和秒為單位預定義對象。
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
現在我們已經了解了 Java 8 的 LocalTime 類,讓我們學習文法。
文法:
public final class LocalTime extends Object Implements Comparable
以上語法是初始化類別的標準方法。如前所述,LocalTime 類別擴展了 Object 類別並實作了類似的介面。
Java LocalTime 的方法
現在讓我們來探索 LocalTime 類別提供的方法。
public LocalDateTime atDate(LocalDate date)
1。 atDate():要建立 LocalDateTime,它將該時間與日期結合。將要組合的日期作為參數,不接受 null。在這裡,所有可能的日期和時間組合都是有效的。
public int compareTo(LocalTime other)
2。 compareTo(): 簡單地將此時間與任何其他經過的時間進行比較。將要比較的另一個時間作為參數。傳回比較值。若較大則為正,若較小則為負。如果另一個值未傳遞或未能傳遞,它會拋出 NullPointerException。
public int get(TemporalField field)
3。 get(): 取得此時的整數值。將要取得的欄位作為參數,不接受 Null。並傳回該欄位的值。傳回值始終在範圍內。如果由於任何原因無法傳回值,則會拋出異常。
4。例外: 如果值超出範圍或無法取得值,則拋出 DateTimeException。如果發生數字溢出,則會拋出 ArithmeticException。
public boolean equals(Object obj)
5。等於: 此時間與任何其他時間之間的簡單比較。它接受一個物件作為參數進行檢查,如果值為 false,則傳回 null。它覆寫類別物件中的 equals。這裡只比較LocalTime類型的物件;如果是其他類型,則傳回 false。
public static LocalTime now()
6。 now(): 在預設時區,now()從系統取得目前時間。 Never Null 始終傳回目前系統時間。
public static LocalTime now(ZoneId zone)
7。 now(ZoneId zone): 與上方類似,但有指定的時區。它以區域 ID 作為參數,不接受 null。傳回指定時區的目前時間。
public static LocalTime of(int hour, int minute, int second, int nanoOfSecond):
8。 of(): 取得 LocalTime 的實例。採用小時、分鐘、秒、奈秒四個參數,並返回本地時間,絕不為空。如果值恰好超出範圍,則會拋出 DateTimeException。
public LocalTime minusHours(long hoursToSubtract):
9。 minusHours(): 傳回此 LocalTime 的副本並減去小時數。以hoursToSubtract為參數;它不能是負數。並傳回此 LocalTime,減去小時數。這裡的實例是不可變的,不受方法呼叫的影響。
public LocalTime plusHours(long hoursToAdd):
10。 plusHours(): 與上面提到的方法完全相反。傳回此 LocalTime 的副本,並新增了指定的小時數。與上方類似,它是不可變的且不受影響。
實作 Java LocalTime 的範例
現在我們已經了解了上面的方法,讓我們嘗試用範例來示範這些方法。
範例#1
代碼:
public class localtime { public static void main(String[] args) { LocalTime time_now = LocalTime.now(); System.out.println(time_now); } }
程式碼解讀:例如例1,我們簡單地實作了LocalTime類別的now()方法。創建了一個類,然後主類後面跟著方法呼叫和簡單的輸出語句。執行後,它將返回當前系統時間。輸出的格式為「小時」、「分鐘」、「秒」和「小秒」。請參閱下面的螢幕截圖以獲取輸出。
輸出:
範例#2
代碼:
import java.time.LocalTime; public class localtime { public static void main(String[] args) { LocalTime time_1 = LocalTime.of(10,43,12); System.out.println(time_1); LocalTime time_2=time_1.minusHours(3); LocalTime time_3=time_2.minusMinutes(41); System.out.println(time_3); } }
Code Interpretation: Here, we have demonstrated two methods, minusHours() and minusMinutes(). After creating our class and main class, we called the () method with parameters and printed the next line’s output. Later, we created two objects with two methods, respectively, for hours and for minutes. So, out of the given time in of() method, the number of hours to be subtracted will be 2, and minutes will be 41. With that in mind, our output should be 10-3 hours, which is 7 hours and 43-41 minutes, which will be 2 minutes. So, the final output must be “07:02:12”. For sample output, refer to the below attached screenshot.
Output:
Example #3
Code:
import java.time.*; public class localtime { public static void main(String[] args) { LocalTime time1 = LocalTime.parse("13:08:00"); LocalTime time_now = LocalTime.now(); System.out.println("LocalTime1: " + time1); System.out.println("LocalTime2: " + time_now); boolean eq_value = time1.equals(time_now); System.out.println("Both times are equal: " + eq_value); } }
Code Interpretation: In our 3rd example, we have implemented the equals method. Other than the creation of class and the main class, we have two objects with assigned values. At first, we have passed a specific time, and for the second, we have fetched the current time of the system with the now() method. Later, we have printed these two values and then a boolean comparison. Using the equals() method, we have compared the two times and passed the output to the output print statement. The equals method’s output will be either true or false; based on the values passed here, the output here must be false. Refer below the screenshot for output.
Output:
Conclusion
Java 8’s Date Time update comes with many features, and LocalTime is one of them, which does not store any value but is a better representation of the date and time. We understood the methods with description and followed by three example samples. LocalTime class of java provides a wide range of methods, more than mentioned and can be used as per requirement.
以上是Java本地時間的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Java在企業級應用中被廣泛使用是因為其平台獨立性。 1)平台獨立性通過Java虛擬機(JVM)實現,使代碼可在任何支持Java的平台上運行。 2)它簡化了跨平台部署和開發流程,提供了更大的靈活性和擴展性。 3)然而,需注意性能差異和第三方庫兼容性,並採用最佳實踐如使用純Java代碼和跨平台測試。

JavaplaysigantroleiniotduetoitsplatFormentence.1)itallowscodeTobewrittenOnCeandrunonVariousDevices.2)Java'secosystemprovidesuseusefidesusefidesulylibrariesforiot.3)

ThesolutiontohandlefilepathsacrossWindowsandLinuxinJavaistousePaths.get()fromthejava.nio.filepackage.1)UsePaths.get()withSystem.getProperty("user.dir")andtherelativepathtoconstructthefilepath.2)ConverttheresultingPathobjecttoaFileobjectifne

Java'splatFormIndenceistificantBecapeitAllowSitallowsDevelostWriTecoDeonCeandRunitonAnyPlatFormwithAjvm.this“ writeonce,runanywhere”(era)櫥櫃櫥櫃:1)交叉plat formcomplibility cross-platformcombiblesible,enablingDeploymentMentMentMentMentAcrAptAprospOspOspOssCrossDifferentoSswithOssuse; 2)

Java適合開發跨服務器web應用。 1)Java的“一次編寫,到處運行”哲學使其代碼可在任何支持JVM的平台上運行。 2)Java擁有豐富的生態系統,包括Spring和Hibernate等工具,簡化開發過程。 3)Java在性能和安全性方面表現出色,提供高效的內存管理和強大的安全保障。

JVM通過字節碼解釋、平台無關的API和動態類加載實現Java的WORA特性:1.字節碼被解釋為機器碼,確保跨平台運行;2.標準API抽像操作系統差異;3.類在運行時動態加載,保證一致性。

Java的最新版本通過JVM優化、標準庫改進和第三方庫支持有效解決平台特定問題。 1)JVM優化,如Java11的ZGC提升了垃圾回收性能。 2)標準庫改進,如Java9的模塊系統減少平台相關問題。 3)第三方庫提供平台優化版本,如OpenCV。

JVM的字節碼驗證過程包括四個關鍵步驟:1)檢查類文件格式是否符合規範,2)驗證字節碼指令的有效性和正確性,3)進行數據流分析確保類型安全,4)平衡驗證的徹底性與性能。通過這些步驟,JVM確保只有安全、正確的字節碼被執行,從而保護程序的完整性和安全性。


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3漢化版
中文版,非常好用

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

WebStorm Mac版
好用的JavaScript開發工具

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