java中的LocalDateTime在輸出畫面上顯示本地日期和時間。顯示時間的預設格式為 YYYY-MM-DD-hh-mm-ss-zzz。顯示日期和時間的不同因素是年、月、日、小時、分鐘、秒和奈秒。可以將日期和時間加上特定的天數,減去一定的天數,最後就可以非常順利地產生輸出了。 LocalDateTime 類別是最終類別;因此,不允許擴展該類別。 LocalDateTime 類別是一個基於值的類,用於使用 equals() 檢查兩個日期和時間是否彼此相等。
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
Java LocalDateTime 的語法
Java LocalDateTime 類別是 java.time 套件的一部分。我們可以透過以下方式建立該類別的java實例。
以下是語法。
import java.time.LocalDateTime;
我們也可以將值傳遞給 LocalDateTime 類別中的 of()。
以下文法如下:
LocalDateTime Idt= LocalDateTime.of(2011,15,6,6,30,50,100000);
我們也可以使用 parse() 並以字串表示形式傳遞時間值。
LocalDateTime Idt= LocalDateTime.parse("2011-11-10T22:11:03.46045");
此外,我們可以透過傳遞 ID 和 Zone ID 資訊來使用 ofInstant()。
LocalDateTime Idt= LocalDateTime.ofInstant(Instant.now(), ZoneId.SystemDefault());
Java中LocalDateTime的方法
Java LocalDateTime 類別中有不同的方法。
- 字串格式(DateTimeFormatter formatter):用於使用指定的格式化程式格式化日期和時間。
- LocalDateTime minusDays(long days): 用於使用特定日期格式化日期和時間,這些日期將從對應的日期中減去。
- LocalDateTime plusDays(long days): 用於在當前日期上添加一定的天數,然後分別列印輸出。
- int get(TemporalField field): 用來取得 int 整數格式的日期和時間值。
- static LocalDateTime now(): 我們使用此方法從當前時區檢索預設日期和時間,該時區作為預設時區。
Java LocalDateTime 範例
下面給出的是提到的例子:
範例#1
在第一個編碼範例中,我們將見證java中使用format()來格式化特定數量的程式碼。
代碼:
import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class LocalDateTimeExample { public static void main(String[] args) { LocalDateTime now = LocalDateTime.now(); System.out.println("Before doing Formatting: " + now); DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss"); String fDTime = now.format(format); System.out.println("After doing Formatting: " + fDTime); } }
輸出:
如範例輸出所示,我們可以看到格式化前後的日期和時間都已顯示。
範例#2
在第二個程式中,我們將看到 minusDays() 的工作原理,即某個日期減去該日期的某些天數,然後最終列印特定的輸出。
代碼:
import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class LocalDateTimeExample2 { public static void main(String[] args) { LocalDateTime dt1 = LocalDateTime.of(2018, 2, 14, 15, 22); LocalDateTime dt2 = dt1.minusDays(100); System.out.println("Before Formatting: " + dt2); DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm"); String fDTime = dt2.format(format); System.out.println("After Formatting: " + fDTime ); } }
在上面的程式碼中,我們減去了一定的天數;在本例中,它是從 2018 年 2 月 14th 日期開始的 100,如程式碼所示。我們在格式化前和格式化後都得到了答案,如下所示。
輸出:
範例#3
plusDays() 函數與 minusDays() 函數非常相似,唯一的區別是它在當前日期上添加天數,而不是減去特定的天數。因此,在此編碼範例中,我們將在現有日期和時間的基礎上添加一定的天數。
代碼:
import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class LocalDateTimeExample3 { public static void main(String[] args) { LocalDateTime dt1 = LocalDateTime.of(2018, 1, 8, 12, 34); LocalDateTime dt2 = dt1.plusDays(60); System.out.println("Before Formatting: " + dt2); DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm"); String fDTime = dt2.format(format); System.out.println("After Formatting: " + fDTime ); } }
在此範例程式碼中,我們將提供的預設日期指定為 2018 年 1 月 8 日期。我們看到增加的天數,即 60 天。當我們為程式碼新增 60 天時,我們看到日期發生了變化,已經是 2018 年 3 月 9 了。時間不變。僅天數發生了變化,將日期從 1 月 8 日移至 3 月 9 日。
輸出:
範例#4
在這個編碼範例中,我們將看到 get() 的功能,我們將分別取得日期和時間的整數值。我們還將看到一個編碼範例來正確說明該程式。
代碼:
import java.time.LocalDateTime; import java.time.temporal.ChronoField; public class LocalDateTimeExample4 { public static void main(String[] args) { LocalDateTime b = LocalDateTime.of(2018, 3, 10, 14, 36); System.out.println(b.get(ChronoField.DAY_OF_WEEK)); System.out.println(b.get(ChronoField.DAY_OF_YEAR)); System.out.println(b.get(ChronoField.DAY_OF_MONTH)); System.out.println(b.get(ChronoField.HOUR_OF_DAY)); System.out.println(b.get(ChronoField.MINUTE_OF_DAY)); } }
範例程式碼按時間順序給出了星期幾、一年中的某一天、一個月中的某一天、一天中的小時和一天中的分鐘。程式呼叫 ChronoField 套件以確保其正確運行並根據需要產生特定輸出。
輸出:
Example #5
In this coding example, we will see the now() in the LocalDateTime class in Java programming language. This program will return the current date and time along with the seconds in the program.
Code:
import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class LocalDateTimeExample5 { public static void main(String[] args) { LocalDateTime dt1 = LocalDateTime.now(); DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss"); String fDTime = dt1.format(format); System.out.println(fDTime); } }
Output:
The output clearly displays the date and time of the current time zone in the format of hh-mm-ss, providing a clear representation.
Conclusion
This article has seen several programs illustrating all the methods and functions inside the LocalDateTime class. Also, we know the syntax of the LocalDateTime class that is present. Beyond this, we also notice the output of several programs. In aeronautical engineering, professionals frequently utilize the LocalDateTime class to maintain and monitor an aircraft’s current date and time, observing any changes in time zones and their impact on the watch time.
以上是Java 本地日期時間的詳細內容。更多資訊請關注PHP中文網其他相關文章!

深入剖析RuoYi框架的Bean依賴注入機制:無需顯式實現類RuoYi框架是一個流行的Java前後端分離框架,其簡潔的代碼...

使用RedisTemplate進行批量查詢時返回值為空的原因及解決方案在使用SpringData...

在Java中如何在同一個Map中使用不同類型的Key在Java編程中,我們經常會使用Map數據結構來存儲鍵值對。然而,有�...

使用Java解密next-auth生成的JWTToken並獲取信息在使用next-auth生成JWT...

Springboot測試時如何解決動態加載Agent警告問題在進行Springboot項目的測試時,你可能會遇到如下警告信息:WARNING:...

JSON序列化與JDK序列化在存儲上的差異探討在編程和數據存儲領域,序列化是將對象轉換為可存儲或傳輸格式的�...

三維空間中兩線段交點坐標的求解本文將探討如何在三維空間中求解兩條線段的交點坐標,特別地,當這兩條線...


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

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

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

Dreamweaver CS6
視覺化網頁開發工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

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