Java中LocalDate、LocalTime和LocalDateTime類的常用方法
在Java環境中,與日期和時間相關的最重要的類有三種:LocalDate、LocalTime和LocalDateTime。Java程式語言提供這些類來處理與日期和時間相關的問題。我們需要匯入Java包`java.time`,它是日期、時間、瞬間和時間持續時間的核心應用程式程式設計介面(API)。
LocalDate、LocalTime和LocalDateTime類的用途:
java.time.* //包含所有類
java.time.LocalDate //用於LocalDate
java.time.LocalDateTime //用於LocalDateTime
java.time.LocalTime //用於LocalTime
java.time.LocalDate
- 此函式儲存一個不含任何時區的日期。格式將符合ISO-8601標準,並採用預設格式。java.time.LocalTime
- 這是一個與ISO 8601日曆系統協同工作的特定系統。我們將獲得不含任何時區和日期的本地時間。java.time.LocalDateTime
- 此函式提供不含任何時區的本地時間和當前日期。在查詢實際本地時間時,它還包含納秒部分。
以下是一個示例:
localDate: 2022-04-11 localTime: 12:15:26.343 localDateTime: 2022-04-11T12:15:26.344
在LocalDate、LocalTime和LocalDateTime類中使用方法的演算法
在這個演算法中,我們將解釋如何在Java環境中使用LocalDate、LocalTime和LocalDateTime類。透過使用這個演算法,我們將構建一些Java語法,這將有助於我們進一步構建一些Java程式碼。
步驟1 - 開始程序。
步驟2 - 宣告輸入輸出流。
步驟3 - 從Java時間包中宣告並匯入LocalDate、LocalTime、LocalDateTime類。
步驟4 - 定義一個主類。
步驟5 - 定義引數字串。
步驟6 - 構造LocalDate類的例項。
步驟7 - 使用now()方法。
步驟8 - 顯示當前日期。
步驟9 - 構造LocalDateTime類的例項。
步驟10 - 使用now()方法。
步驟11 - 構造LocalTime類。
步驟12 - 使用now()方法。
步驟13 - 宣告plusHours方法。
步驟14 - 獲取返回值。
步驟15 - 終止程序。
在LocalDate、LocalTime和LocalDateTime類中使用方法的語法
LocalDate localDate = LocalDate.now(); System.out.println("localDate Today: "+localDate); LocalTime localTime =LocalTime.now(); System.out.println("localTime Today: "+localTime); LocalDateTime localDateTime =LocalDateTime.now(); System.out.println("localDateTime Today: "+localDateTime); LocalDate localDate = LocalDate.of(2023,04,11); System.out.println("localDate Today: "+localDate); LocalTime localTime =LocalTime.of(23,12,56,234); System.out.println("localTime Today: "+localTime); LocalDateTime localDateTime =LocalDateTime.of(2023,12,01,23,12,56,234); System.out.println("localDateTime Today: "+localDateTime); LocalDate localDateNov = LocalDate.of(2023, Month. APRIL,11); System.out.println("localDateApril: "+localDateNov); System.out.println("Today: "+LocalDate.now()); LocalDate todayPlus10Days=LocalDate.now().plusDays(10); System.out.println("localDatePlus10: "+todayPlus10Days); System.out.println("Time Now: "+LocalTime.now()); LocalTime nowMinus20Minutes=LocalTime.now().minusMinutes(20); System.out.println("nowMinus20Minutes: "+nowMinus20Minutes); System.out.println("Date-Time Now: "+LocalDateTime.now()); LocalDateTime nowPlus2Years =LocalDateTime.now().plusYears(2); System.out.println("todayPlus2Years: "+nowPlus2Years); public static void main(String[] args) { LocalDate date = LocalDate.now(); LocalDate yesterday = date.minusDays(1); LocalDate tomorrow = yesterday.plusDays(2); System.out.println("Today date: "+date); System.out.println("Yesterday date: "+yesterday); System.out.println("Tomorrow date: "+tomorrow); }
在這個語法中,我們嘗試向您展示如何使用LocalDate、LocalTime和LocalDateTime方法類來獲取其實際值。透過使用上述語法,我們將構建一些Java程式碼來有效地解釋問題陳述。
遵循的方法
方法1 - Java程式演示LocalDate、LocalTime和LocalDateTime類中常用的方法
方法2 - Java程式透過使用各種方法來說明LocalDate、LocalTime和LocalDateTime類中常用的方法
方法3 - Java程式透過使用Instant和Temporal方法來說明LocalDate、LocalTime和LocalDateTime類中常用的方法
方法1:使用LocalDate、LocalTime和LocalDateTime類
在這種方法中,我們將嘗試使用特定LocalDate類的now()方法。在這裡,我們可以根據系統時鐘和預設時區獲取LocalDate的值。
示例
//Java Program to demonstrate the Commonly used methods in classes LocalDate, LocalTime and LocalDateTime Classes import java.io.*; import java.time.LocalDate; import java.time.LocalTime; import java.time.LocalDateTime; public class ARBRDD { public static void main (String[] args) { LocalDate presentDate = LocalDate.now(); System.out.println(presentDate); LocalDateTime present = LocalDateTime.now(); System.out.println(present); LocalTime presentTime = LocalTime.now(); System.out.println(presentTime); } }
輸出
2023-04-11 2023-04-11T13:25:11.545488429 13:25:11.545575593
方法2:使用帶有GetDay函式的LocalDate、LocalTime和LocalDateTime類
在這種方法中,我們將討論一些與LocalDate、LocalDateTime、LocalTime類相關的實用程式方法。它們是:
getDayOfMonth()
- 返回月份中的某一天getDayOfWeek()
- 返回星期幾getDayOfYear()
- 返回一年中的某一天getMonth()
- 返回月份的名稱getMonthValue()
- 返回月份的數值
示例
//Java Program to illustrate Commonly used methods in classes LocalDate, LocalTime and LocalDateTime Classes by using various methods import java.io.*; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; public class ARBRDD { public static void main(String[] args){ LocalDate presentDate = LocalDate.now(); System.out.println(presentDate); LocalDateTime present = LocalDateTime.now(); System.out.println(present); LocalTime presentTime = LocalTime.now(); System.out.println(presentTime); System.out.println(presentDate.getDayOfMonth()); System.out.println(presentDate.getDayOfWeek()); System.out.println(presentDate.getDayOfYear()); System.out.println(presentDate.getMonth()); System.out.println(presentDate.getMonthValue()); System.out.println(presentDate.isLeapYear()); System.out.println(presentDate.lengthOfYear()); System.out.println(presentDate.lengthOfMonth()); System.out.println(presentDate.plusDays(50)); System.out.println(presentDate.plusMonths(50)); System.out.println(presentDate.plusYears(50)); System.out.println(presentDate.minusDays(50)); System.out.println(presentDate.minusMonths(50)); System.out.println(presentDate.minusYears(50)); System.out.println(present.plusHours(100)); System.out.println(present.plusMinutes(1000)); System.out.println(present.plusSeconds(100000)); System.out.println(present.plusNanos(1000000)); } }
輸出
2023-04-11 2023-04-11T13:29:55.830090 13:29:55.831305 11 TUESDAY101 APRIL 4 false 365 30 2023-05-31 2027-06-11 2073-04-11 2023-02-20 2019-02-11 1973-04-11 2023-04-15T17:29:55.830090 2023-04-12T06:09:55.830090 2023-04-12T17:16:35.830090 2023-04-11T13:29:55.831090
方法3:使用Instant和Temporal方法獲取時間、日期和日期時間
Instant方法的使用
在這個Java示例中,我們嘗試向您展示如何實現Instant類以及Temporal方法來獲取本地日期和時間,而無需提及使用者的當前時區。
示例
//Java Program to illustrate Commonly used methods in classes LocalDate, LocalTime and LocalDateTime Classes by using the instant method package com.howtodoinjava.core.datetime; import java.time.Duration; import java.time.Instant; import java.time.LocalDateTime; import java.time.temporal.ChronoUnit; import java.util.Calendar; import java.util.Date; public class AddHoursMinutesSecondsToDate { public static void main(final String[] args) { //1 - Same methodss for ZonedDateTime and OffsetDateTime LocalDateTime now = LocalDateTime.now(); System.out.println("Current Date and Time = " + now); LocalDateTime updatedTime = now.plusHours(2); System.out.println("Updated Date and Time = " + updatedTime); updatedTime = now.plusMinutes(20); System.out.println("Updated Date and Time = " + updatedTime); updatedTime = now.plus(Duration.ofMillis(8000)); System.out.println("Updated Date and Time = " + updatedTime); updatedTime = now.plus(20, ChronoUnit.HOURS); System.out.println("Updated Date and Time = " + updatedTime); updatedTime = now.minusHours(2); updatedTime = now.minusMinutes(20); updatedTime = now.minusSeconds(300); updatedTime = now.minus(Duration.ofMillis(8000)); updatedTime = now.minus(20, ChronoUnit.HOURS); Instant currentInstant = Instant.parse("2022-06-24T05:12:35Z"); System.out.println("Current Date and Time = " + currentInstant); Instant updatedInstant = currentInstant.plus(2, ChronoUnit.HOURS); System.out.println("Updated Date and Time = " + updatedInstant); updatedInstant = currentInstant.plusSeconds(300); updatedInstant = currentInstant.plusMillis(8000); updatedInstant = currentInstant.plusNanos(600000); updatedInstant = currentInstant.plusSeconds(300); System.out.println("Updated Date and Time = " + updatedInstant); //2 java.util.Date Date date = new Date(); Calendar cal = Calendar.getInstance(); cal.setTime(date); System.out.println("Current Date and Time = " + cal.getTime()); cal.add(Calendar.HOUR, 2); System.out.println("Updated Date and Time = " + cal.getTime()); cal.add(Calendar.MINUTE, -15); System.out.println("Updated Date and Time = " + cal.getTime()); cal.add(Calendar.SECOND, 10); System.out.println("Updated Date and Time = " + cal.getTime()); } }
輸出
Current Date and Time = 2023-04-11T13:32:32.989642529 Updated Date and Time = 2023-04-11T15:32:32.989642529 Updated Date and Time = 2023-04-11T13:52:32.989642529 Updated Date and Time = 2023-04-11T13:32:40.989642529 Updated Date and Time = 2023-04-12T09:32:32.989642529 Current Date and Time = 2022-06-24T05:12:35Z Updated Date and Time = 2022-06-24T07:12:35Z Updated Date and Time = 2022-06-24T05:17:35Z Current Date and Time = Tue Apr 11 13:32:32 GMT 2023 Updated Date and Time = Tue Apr 11 15:32:32 GMT 2023 Updated Date and Time = Tue Apr 11 15:17:32 GMT 2023 Updated Date and Time = Tue Apr 11 15:17:42 GMT 2023
Instant方法的使用
在這個Java示例中,我們嘗試向您展示如何實現Temporal方法來獲取本地日期和時間,而無需提及使用者的當前時區。
示例
//Java Program to illustrate Commonly used methods in classes LocalDate, LocalTime and LocalDateTime Classes by using the temporal method package com.howtodoinjava.core.datetime; import java.time.LocalDate; import java.time.temporal.ChronoUnit; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class DaysBetweenDates { public static void main(final String[] args) { LocalDate date1 = LocalDate.now(); LocalDate date2 = date1.plusDays(99); long diffInDays = ChronoUnit.DAYS.between(date1, date2); System.out.println(diffInDays); diffInDays = date1.until(date2, ChronoUnit.DAYS); System.out.println(diffInDays); LocalDate startDate = LocalDate.now(); LocalDate endDate = startDate.plusMonths(2); long numOfDays = ChronoUnit.DAYS.between(startDate, endDate); List<LocalDate> listOfDates = Stream.iterate(startDate, date -> date.plusDays(1)) .limit(numOfDays) .collect(Collectors.toList()); System.out.println(listOfDates); } }
輸出
99 99 [2023-04-11, 2023-04-12, 2023-04-13, 2023-04-14, 2023-04-15, 2023-04-16, 2023- 04-17, 2023-04-18, 2023-04-19, 2023-04-20, 2023-04-21, 2023-04-22, 2023-04-23, 2023-04-24, 2023-04-25, 2023-04-26, 2023-04-27, 2023-04-28, 2023-04-29, 2023- 04-30, 2023-05-01, 2023-05-02, 2023-05-03, 2023-05-04, 2023-05-05, 2023-05-06, 2023-05-07, 2023-05-08, 2023-05-09, 2023-05-10, 2023-05-11, 2023-05-12, 2023- 05-13, 2023-05-14, 2023-05-15, 2023-05-16, 2023-05-17, 2023-05-18, 2023-05-19, 2023-05-20, 2023-05-21, 2023-05-22, 2023-05-23, 2023-05-24, 2023-05-25, 2023- 05-26, 2023-05-27, 2023-05-28, 2023-05-29, 2023-05-30, 2023-05-31, 2023-06-01, 31 2023-06-02, 2023-06-03, 2023-06-04, 2023-06-05, 2023-06-06, 2023-06-07, 2023- 06-08, 2023-06-09, 2023-06-10]
結論
java.time
類是不可變類,它們以預設格式表示日期和時間的數值。在今天的這篇文章中,我們嘗試使用演算法構建一些Java語法,然後演示一些可能的Java程式碼來有效地解釋問題陳述。