Home  >  Article  >  Java  >  Java DayOfWeek

Java DayOfWeek

王林
王林Original
2024-08-30 15:52:29708browse

Java DayOfWeek class comprises packages that include the DayOfWeekenums and APIs, which support different time zones and various rules related to get the specific day or time according to the requirement of the logic or the implementation of the code. This class is useful for accessing different date and time using various fields, values, units, date, and time calendars that can provide exact timing-related issues for the programmers needed as part of the implementation logic or provide useful data for retrieving the status of the system.

Syntax:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

public class <class_name><method_name><arguments>
public DayOfWeek Temporal adjustInto (Temporal temporal)

Methods of DayOfWeek in Java

Java DayOfWeek includes many methods, which are as follows:

1. Temporal

Syntax:

public Temporal adjustInto(Temporal temporal)

This method is used for adjusting the temporal object specific to have that specific day of this week. It returns an object which has the same behavior as that of the input given, which is the same as the value given at the run time.

Example: This program is used to demonstrate the adjusted method of the temporal class.

Code:

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

Output:

Java DayOfWeek

2. Static DayOfWeek

Syntax:

public static DayOfWeekfrom(TemporalAccessor temporal)

This method is used to obtain an instance or object which is related to the DayOfWeek class passing the argument as TemporalAccessor.

Example: This program is used to demonstrate the DayOfWeekfrom() method as part of static.

Code:

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

Output:

Java DayOfWeek

3. int

Syntax:

public intget(TemporalField field)

This method is used for getting the specified value of the field from the day of the week, which is an int value when passed as an argument with an integer value. In case no value is obtained from that field, or the value lies out of the range, then the field throws DateTimeException.

Example: This program demonstrates the public int get(TemporalField field) method.

Code:

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

Output:

Java DayOfWeek

4. String

Syntax:

public String getDisplayName(TextStyle style,Locale locale)

As the name suggests, this method is primarily used for displaying or for textual representation such as ‘Mon’ or ‘Friday’ and returns the textual name used for identification and is basically provided as an input by the programmers for a convenient representation in order to get the textual representation as desired. In case no locale or textual representation is done, it will most likely return a numeric value.

Example: This program demonstrates the String getDisplayName(TextStyle style, Locale locale)

Code:

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

Output:

Java DayOfWeek

5. long

Syntax:

public long getLong(TemporalField field)

This method is used to get the value after passing the parameter with the day of the week as long. This queries the value with the specified values and returns a value if not supported, then returns and throws an exception.

Example: This program demonstrates the public long getLong(TemporalField field).

Code:

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

Output:

Java DayOfWeek

6. Boolean

Syntax:

public booleanisSupported(TemporalField field)

It keeps a check and verifies whether the field gets supported, or just it returns a null value in case it does not get supported. Supports and checks with the boolean values of true or false.

Example: This program demonstrates the public booleanisSupported(TemporalField field).

Code:

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

Output

Java DayOfWeek

7. DayOfWeek

Syntax:

public DayOfWeekminus(long days)

This method returns the day-of-week, which is specified before the actual day, then the number of days present in the DayOfWeek actual date schedule.

Example: This program demonstrates the public DayOfWeek minus(long days).

Code:

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

Output

Java DayOfWeek

8. Static DayOfWeek

Syntax:

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:

Java DayOfWeek

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:

Java DayOfWeek

10. R

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:

Java DayOfWeek

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:

Java DayOfWeek

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:

Java DayOfWeek

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:

Java DayOfWeek

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.

The above is the detailed content of Java DayOfWeek. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Java DurationNext article:Java Duration