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中文网其他相关文章!