Java DayOfWeek 類別由包含 DayOfWeekenum 和 API 的套件組成,支援不同時區以及根據邏輯或程式碼實現的要求來取得特定日期或時間的各種規則。此類別可用於使用各種欄位、值、單位、日期和時間日曆存取不同的日期和時間,這些欄位、值、單位、日期和時間日曆可以為作為實現邏輯一部分所需的程式設計師提供精確的計時相關問題,或提供用於檢索狀態的有用資料。系統。
文法:
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
public class <class_name><method_name><arguments> public DayOfWeek Temporal adjustInto (Temporal temporal)</arguments></method_name></class_name>
Java 中 DayOfWeek 的方法
Java DayOfWeek 包含許多方法,如下:
1.顳部
文法:
public Temporal adjustInto(Temporal temporal)
此方法用於調整特定的時間物件以具有本週的特定日期。它傳回一個與給定輸入具有相同行為的對象,該對象與運行時給定的值相同。
範例:本程式用於示範時態類別的調整方法。
代碼:
import java.util.*; import java.time.*; public class Adjust_Into { public static void main(String[] args) { ZonedDateTimedt = ZonedDateTime.now(); System.out.println(dt); LocalDate dt2 = LocalDate.parse("2020-05-01"); dt = (ZonedDateTime)dt2.adjustInto(dt); System.out.println(dt); } }
輸出:
2.靜態星期幾
文法:
public static DayOfWeekfrom(TemporalAccessor temporal)
此方法用於取得與 DayOfWeek 類別相關的實例或對象,並將參數作為 TemporalAccessor 傳遞。
範例:此程式用於示範 DayOfWeekfrom() 方法作為靜態的一部分。
代碼:
import java.time.DayOfWeek; import java.time.LocalDate; import java.time.Month; public class D_Of_Wk { public static void main(String[] args) { LocalDatelcldt = LocalDate.of(2000, Month.APRIL, 23); DayOfWeekdofwk = DayOfWeek.from(lcldt); System.out.println("Current_day_of_the_week " + lcldt + " ->> " + dofwk.name()); } }
輸出:
3.整數
文法:
public intget(TemporalField field)
此方法用於從星期幾取得欄位的指定值,當作為整數值的參數傳遞時,該值是 int 值。如果沒有從該欄位取得任何值,或者該值超出範圍,則該欄位將拋出 DateTimeException。
範例:此程式示範了 public int get(TemporalField field) 方法。
代碼:
import java.time.LocalDate; import java.time.temporal.ChronoField; public class Date_Get { public static void main(String[] args) { LocalDate dt2 = LocalDate.parse("2020-06-18"); System.out.println(dt2.get(ChronoField.MONTH_OF_YEAR)); } }
輸出:
4.字串
文法:
public String getDisplayName(TextStyle style,Locale locale)
顧名思義,該方法主要用於顯示或文字表示,例如“星期一”或“星期五”,並返回用於識別的文字名稱,基本上由程式設計師作為輸入提供,以方便表示為了獲得所需的文字表示。如果沒有完成語言環境或文字表示,它很可能會傳回一個數值。
範例:此程式示範了 String getDisplayName(TextStyle style, Locale locale)
代碼:
import java.time.Month; import java.time.format.TextStyle; import java.util.Locale; public class Mnth_en { public static void main(String[] args) { Month mnth = Month.of(5); System.out.println(mnth.getDisplayName(TextStyle.SHORT, Locale.ENGLISH)); } }
輸出:
5.長
文法:
public long getLong(TemporalField field)
此方法用於取得傳入參數後與星期幾一樣長的值。這會查詢指定值的值,如果不支援則傳回一個值,然後傳回並拋出異常。
範例:此程式示範了 public long getLong(TemporalField field)。
代碼:
import java.time.LocalTime; import java.time.temporal.ChronoField; public class Gt_Dt_Time_Ex { public static void main(String[] args) { LocalTimetm =LocalTime.parse("12:28:39.20"); long scnd_vl = tm.getLong(ChronoField.MILLI_OF_SECOND); System.out.println("Mli_Sec_Field: " + scnd_vl); } }
輸出:
6.布爾
文法:
public booleanisSupported(TemporalField field)
它會檢查並驗證該欄位是否受支持,或只是傳回一個空值,以防它不受支援。支援並檢查 true 或 false 的布林值。
範例:此程式示範公用布林值Supported(TemporalField 欄位)。
代碼:
import java.time.YearMonth; import java.time.temporal.ChronoField; public class Dt_It_pl { public static void main(String[] args) { YearMonthtdYrMn = YearMonth.of(2020, 8); System.out.println("YearMonth :" + tdYrMn); booleanvl = tdYrMn.isSupported(ChronoField.EPOCH_DAY); System.out.println("EPOCH_DAY is considered as one of the fields of YearMonthClass. " + vl); } }
輸出
7.星期幾
文法:
public DayOfWeekminus(long days)
此方法傳回實際日期之前指定的星期幾,然後傳回 DayOfWeek 實際日期計畫中存在的天數。
範例:此程式示範公共 DayOfWeek 減去(長天)。
代碼:
import java.time.DayOfWeek; public class Day_of_wk_Minus { public static void main(String[] args) { DayOfWeekdy_Of_wk = DayOfWeek.of(4); System.out.println("Day_Week : "+ dy_Of_wk.name() + " - "+ dy_Of_wk.getValue()); long av = 10; dy_Of_wk = dy_Of_wk.minus(av); System.out.println("Day_Week_Earlier " + av + " sm_days: " + dy_Of_wk.name() + " - " + dy_Of_wk.getValue()); } }
輸出
8.靜態星期幾
文法:
public static DayOfWeekof(intdayOfWeek)
It is used to display the 7 days of the week for an enum representation and then is used to obtain an instance of DayOfWeek from an int value.
Example: This program demonstrates the public static DayOfWeek of(intdayOfWeek).
Code:
import java.time.DayOfWeek; public class Dy_Of_Wk_Ex { public static void main(String[] args) { DayOfWeekdyOfWk = DayOfWeek.of(7); System.out.println("Day-Of_the_Week - "+ dyOfWk.name()); System.out.println("Int Value of " + dyOfWk.name() + " - " + dyOfWk.getValue()); } }
Output:
9. DayOfWeek
Syntax:
public DayOfWeekplus(long days)
In this method, the calculation structure revolves around getting the days and the values specified by a number of days after the specified value.
Example: This program demonstrates the public DayOfWeek plus(long days).
Code:
import java.time.DayOfWeek; import java.time.LocalDate; import java.time.Month; public class Dy_Of_Wk_Pls { public static void main(String[] args) { LocalDatelcldt = LocalDate.of(2020, Month.JANUARY, 22); DayOfWeekdyOfWk = DayOfWeek.from(lcldt); System.out.println(dyOfWk.getValue()); dyOfWk = dyOfWk.plus(3); System.out.println(dyOfWk.getValue()); } }
Output:
10. R
Syntax:
public <r> R query(TemporalQuery<r> query)</r></r>
The specified strategy object is used for querying a query using the specific query for the queried object, and it can also return a null value throws an exception in case of an undefined or not proper query.
Example: This program demonstrates the public
Code:
import java.time.LocalDate; import java.time.temporal.TemporalQueries; public class Locl_Test_Av { public static void main(String[] args) { LocalDate dt1 = LocalDate.parse("2020-12-04"); System.out.printf("Value of Locl_date", dt1.query(TemporalQueries.precision())); } }
Output:
11. ValueRange
Syntax:
public ValueRangerange(TemporalField field)
This ValueRange method gets the range of some valid values for the specified field, which consists of range objects, and then the range of valid value is not null that also needs to keep a check on.
Example: This program demonstrates the public ValueRange range(TemporalField field).
Code:
import java.time.LocalTime; import java.time.temporal.ChronoField; import java.time.temporal.ValueRange; public class GField_For_Umk { public static void main(String[] args) { LocalTime lk_1 = LocalTime.parse("10:22:06"); ValueRange result = lk_1.range(ChronoField.CLOCK_HOUR_OF_AMPM); System.out.println("CLOCK_HOUR_OF_AMPM: " + result); } }
Output:
12. static DayOfWeek
Syntax:
public static DayOfWeekof(intdayOfWeek)
This method returns specific enum type constants with specified names used for matching the identifiers with the declared enums and values as designated to the method.
Example: This program demonstrates the public static DayOfWeek of(intdayOfWeek).
Code:
import java.time.DayOfWeek; public class Dy_Of_Week_Ging { public static void main(String[] args) { DayOfWeekdy_Of_wk = DayOfWeek.of(4); System.out.println("Dy_Of_the_week -" + dy_Of_wk.name()); System.out.println("int_val_of" + dy_Of_wk.name() + " - " + dy_Of_wk.getValue()); } }
Output:
13. static DayOfWeek[]
Syntax:
public static DayOfWeekof(intdayOfWeek)
This method is specifically used to obtain an instance which is the DayOfWeek instance, from an int value and returns a singleton day-of-week which is not null. It throws an exception if the day-week-of is invalid.
Example: This program demonstrates the public static DayOfWeek of(intdayOfWeek)
Code:
import java.time.DayOfWeek; public class Dy_Of_Week_Int { public static void main(String[] args) { DayOfWeekdy_Of_wk = DayOfWeek.of(6); System.out.println("Day_Of_The_Wk - "+ dy_Of_wk.name()); System.out.println("Int_Vl_Of " + dy_Of_wk.name() + " - "+ dy_Of_wk.getValue()); } }
Output:
Conclusion
This class is very useful in terms of programming where there are huge requirements and something related to the time-bound components or determine which timing constraints are needed for the implementation, which indicates any system or project with timings. Date of the week or month any module is fully associated with Java and DayOfWeek.
以上是Java 星期幾的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本文討論了使用Maven和Gradle進行Java項目管理,構建自動化和依賴性解決方案,以比較其方法和優化策略。

本文使用Maven和Gradle之類的工具討論了具有適當的版本控制和依賴關係管理的自定義Java庫(JAR文件)的創建和使用。

本文討論了使用咖啡因和Guava緩存在Java中實施多層緩存以提高應用程序性能。它涵蓋設置,集成和績效優勢,以及配置和驅逐政策管理最佳PRA

本文討論了使用JPA進行對象相關映射,並具有高級功能,例如緩存和懶惰加載。它涵蓋了設置,實體映射和優化性能的最佳實踐,同時突出潛在的陷阱。[159個字符]

Java的類上載涉及使用帶有引導,擴展程序和應用程序類負載器的分層系統加載,鏈接和初始化類。父代授權模型確保首先加載核心類別,從而影響自定義類LOA

本文解釋了用於構建分佈式應用程序的Java的遠程方法調用(RMI)。 它詳細介紹了接口定義,實現,註冊表設置和客戶端調用,以解決網絡問題和安全性等挑戰。

本文詳細介紹了用於網絡通信的Java的套接字API,涵蓋了客戶服務器設置,數據處理和關鍵考慮因素,例如資源管理,錯誤處理和安全性。 它還探索了性能優化技術,我

本文詳細介紹了創建自定義Java網絡協議。 它涵蓋協議定義(數據結構,框架,錯誤處理,版本控制),實現(使用插座),數據序列化和最佳實踐(效率,安全性,維護


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

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

Dreamweaver Mac版
視覺化網頁開發工具

禪工作室 13.0.1
強大的PHP整合開發環境

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

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