首頁  >  文章  >  Java  >  Java本地時間

Java本地時間

WBOY
WBOY原創
2024-08-30 15:49:48630瀏覽

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()方法。創建了一個類,然後主類後面跟著方法呼叫和簡單的輸出語句。執行後,它將返回當前系統時間。輸出的格式為「小時」、「分鐘」、「秒」和「小秒」。請參閱下面的螢幕截圖以獲取輸出。

輸出:

Java本地時間

範例#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:

Java本地時間

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:

Java本地時間

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中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:Java即時下一篇:Java即時