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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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

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

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

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

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

Spring Boot simplifies the creation of robust, scalable, and production-ready Java applications, revolutionizing Java development. Its "convention over configuration" approach, inherent to the Spring ecosystem, minimizes manual setup, allo
