Java DayOfWeek
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:

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:

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:

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:

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:

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

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

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:

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)
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:

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.
The above is the detailed content of Java DayOfWeek. For more information, please follow other related articles on the PHP Chinese website!
Hot AI Tools
Undresser.AI Undress
AI-powered app for creating realistic nude photos
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undress AI Tool
Undress images for free
Clothoff.io
AI clothes remover
AI Hentai Generator
Generate AI Hentai for free.
Hot Article
Hot Tools
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
1378
52
Perfect Number in Java
Aug 30, 2024 pm 04:28 PM
Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.
Random Number Generator in Java
Aug 30, 2024 pm 04:27 PM
Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.
Weka in Java
Aug 30, 2024 pm 04:28 PM
Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.
Smith Number in Java
Aug 30, 2024 pm 04:28 PM
Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.
Java Spring Interview Questions
Aug 30, 2024 pm 04:29 PM
In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.
Break or return from Java 8 stream forEach?
Feb 07, 2025 pm 12:09 PM
Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is
TimeStamp to Date in Java
Aug 30, 2024 pm 04:28 PM
Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.
Java Program to Find the Volume of Capsule
Feb 07, 2025 am 11:37 AM
Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4


