Java DayOfWeek 類別由包含 DayOfWeekenum 和 API 的套件組成,支援不同時區以及根據邏輯或程式碼實現的要求來取得特定日期或時間的各種規則。此類別可用於使用各種欄位、值、單位、日期和時間日曆存取不同的日期和時間,這些欄位、值、單位、日期和時間日曆可以為作為實現邏輯一部分所需的程式設計師提供精確的計時相關問題,或提供用於檢索狀態的有用資料。系統。
文法:
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
public class <class_name><method_name><arguments> public DayOfWeek Temporal adjustInto (Temporal temporal)
Java DayOfWeek 包含許多方法,如下:
文法:
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); } }
輸出:
文法:
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()); } }
輸出:
文法:
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)); } }
輸出:
文法:
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)); } }
輸出:
文法:
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); } }
輸出:
文法:
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); } }
輸出
文法:
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()); } }
輸出
文法:
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:
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:
Syntax:
public <R> R query(TemporalQuery<R> query)
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 <R> R query(TemporalQuery<R> query).
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:
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:
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:
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:
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中文網其他相關文章!