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

Java 本地日期時間

WBOY
WBOY原創
2024-08-30 15:52:00804瀏覽

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);
}
}

輸出:

如範例輸出所示,我們可以看到格式化前後的日期和時間都已顯示。

Java 本地日期時間

範例#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,如程式碼所示。我們在格式化前和格式化後都得到了答案,如下所示。

輸出:

Java 本地日期時間

範例#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 日。

輸出:

Java 本地日期時間

範例#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 套件以確保其正確運行並根據需要產生特定輸出。

輸出:

Java 本地日期時間

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.

Java 本地日期時間

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

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