The Java DayOfWeek
enum is a built-in enumeration that represents the seven days of the week. It is part of the java.time
package introduced in Java 8, which provides a comprehensive date and time API.
The DayOfWeek
enum consists of seven constants, one for each day of the week: MONDAY
, TUESDAY
, WEDNESDAY
, THURSDAY
, FRIDAY
, SATURDAY
, and SUNDAY
.
Here is an example of how to use the DayOfWeek
enum:
import java.time.DayOfWeek; import java.time.LocalDate; public class Example { public static void main(String[] args) { LocalDate date = LocalDate.now(); DayOfWeek dayOfWeek = date.getDayOfWeek(); System.out.println("Today is " + dayOfWeek); } }
In this example, we get the current date using LocalDate.now()
, and then get the DayOfWeek
of that date using date.getDayOfWeek()
. We then print out the result using System.out.println()
. This will output something like “Today is WEDNESDAY”.
Java DayOfWeek enum Declaration:
The DayOfWeek
enum in Java is declared as follows:
public enum DayOfWeek implements TemporalAccessor, TemporalAdjuster, Comparable<DayOfWeek> { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY; }
Let’s break down this declaration:
- The
public
keyword makes the enum accessible from any class in any package. - The
enum
keyword specifies that this is an enumeration. - The name of the enum is
DayOfWeek
. - The
implements
keyword is used to specify that theDayOfWeek
enum implements theTemporalAccessor
,TemporalAdjuster
, andComparable<DayOfWeek>
interfaces. TheTemporalAccessor
interface allows the enum to be queried for date and time information, while theTemporalAdjuster
interface allows the enum to be adjusted to a different date or time. TheComparable
interface allowsDayOfWeek
objects to be compared to each other. - The enum constants are declared using uppercase letters and are separated by commas. In this case, the constants are
MONDAY
,TUESDAY
,WEDNESDAY
,THURSDAY
,FRIDAY
,SATURDAY
, andSUNDAY
.
Note that enum constants are implicitly static and final, so they can be accessed using the syntax DayOfWeek.MONDAY
, for example.
Enum Constants:
In Java, an enum is a special data type that represents a fixed set of constants. Each constant is an instance of the enum, and they are declared using the enum
keyword. Enum constants are typically used to represent a set of related values that are known at compile time.
Here is an example of an enum declaration:
enum Size { SMALL, MEDIUM, LARGE, EXTRA_LARGE }
In this example, the Size
enum has four constants: SMALL
, MEDIUM
, LARGE
, and EXTRA_LARGE
. These constants are defined using uppercase letters, and are separated by commas. Each constant is an instance of the Size
enum.
Enum constants can have associated values and behavior. For example, you could define an enum like this:
enum Size { SMALL("S"), MEDIUM("M"), LARGE("L"), EXTRA_LARGE("XL"); private String abbreviation; private Size(String abbreviation) { this.abbreviation = abbreviation; } public String getAbbreviation() { return abbreviation; } }
In this example, each Size
constant has an associated abbreviation. The abbreviation
field is private, so it can only be accessed from within the enum. The Size
constructor takes an abbreviation argument and assigns it to the abbreviation
field. The getAbbreviation
method returns the abbreviation for a given Size
constant.
Enum constants can also have methods and implement interfaces, just like regular classes. This makes enums a powerful tool for defining types that represent a fixed set of related values.
Methods of Java DayOfWeek:
The DayOfWeek
enum in Java provides several methods to work with instances of this enumeration. Here are some of the most commonly used methods:
valueOf(String name)
: This method returns theDayOfWeek
constant with the specified name. The name must match the name of one of the constants in the enum, and it is case-sensitive.
DayOfWeek wednesday = DayOfWeek.valueOf("WEDNESDAY");
values()
: This method returns an array of all theDayOfWeek
constants, in the order they were declared.
DayOfWeek[] daysOfWeek = DayOfWeek.values();
getDisplayName(TextStyle style, Locale locale)
: This method returns the display name of theDayOfWeek
in the specified style and locale. Thestyle
argument specifies the style of the name, and it can be one ofFULL
,SHORT
, orNARROW
. Thelocale
argument specifies the locale to use for the display name.
String name = DayOfWeek.MONDAY.getDisplayName(TextStyle.FULL, Locale.US);
plus(long days)
: This method returns theDayOfWeek
that is a specified number of days after the currentDayOfWeek
. Thedays
argument specifies the number of days to add, and it can be negative.
DayOfWeek friday = DayOfWeek.MONDAY.plus(4);
minus(long days)
: This method returns theDayOfWeek
that is a specified number of days before the currentDayOfWeek
. Thedays
argument specifies the number of days to subtract, and it can be negative.
DayOfWeek saturday = DayOfWeek.MONDAY.minus(2);
These are just a few of the methods available in the DayOfWeek
enum. You can find more methods in the Java documentation for the DayOfWeek
class.
Methods inherited from class java.lang.Enum:
As an enum
in Java is implicitly a subclass of Enum
class, it inherits several methods from the Enum
class. Here are some of the most commonly used methods that are inherited by all Java enums:
name()
: This method returns the name of the enum constant as aString
. The name is the same as the identifier used when declaring the constant.
String name = DayOfWeek.MONDAY.name();
ordinal()
: This method returns the position of the enum constant in the enum declaration, starting from 0. The order of the constants in the enum declaration determines their ordinal value.
int ordinal = DayOfWeek.MONDAY.ordinal();
compareTo(E other)
: This method compares the ordinal value of the enum constant with the ordinal value of another enum constant. It returns a negative value if this constant is less than the other constant, zero if they are equal, or a positive value if this constant is greater than the other constant.
int compare = DayOfWeek.MONDAY.compareTo(DayOfWeek.TUESDAY);
equals(Object other)
: This method checks if this enum constant is equal to another object. It returnstrue
if the other object is also an enum constant of the same enum type and has the same ordinal value.
boolean equal = DayOfWeek.MONDAY.equals(DayOfWeek.MONDAY);
valueOf(Class<T> enumType, String name)
: This method returns the enum constant with the specified name, of the specified enum type. It throws anIllegalArgumentException
if the specified name does not match any of the constants in the enum.
DayOfWeek wednesday = Enum.valueOf(DayOfWeek.class, "WEDNESDAY");
These are some of the most commonly used methods inherited from the Enum
class in Java. You can find more methods in the Java documentation for the Enum
class.
Java DayOfWeek Example: get()
The get()
method of the DayOfWeek
enum in Java returns the numeric value of the day-of-week, where 1 represents Monday and 7 represents Sunday. Here’s an example that demonstrates how to use the get()
method:
import java.time.DayOfWeek; public class DayOfWeekExample { public static void main(String[] args) { DayOfWeek today = DayOfWeek.MONDAY; int dayNumber = today.get(); System.out.println("Today is " + today + ", which is day number " + dayNumber + " of the week."); } }
In this example, we create a DayOfWeek
object representing Monday and assign it to the today
variable. We then call the get()
method on the today
object to get the numeric value of the day-of-week, and assign it to the dayNumber
variable. Finally, we print a message that includes the day-of-week name and its corresponding day number.
When we run this program, the output will be:
Today is MONDAY, which is day number 1 of the week.
This shows that Monday has a day number of 1 in the DayOfWeek
enum.
Java DayOfWeek Example: of()
The of()
method of the DayOfWeek
enum in Java returns the enum constant for the specified day-of-week. The argument passed to the of()
method should be an integer value between 1 and 7, where 1 represents Monday and 7 represents Sunday. Here’s an example that demonstrates how to use the of()
method:
import java.time.DayOfWeek; public class DayOfWeekExample { public static void main(String[] args) { int dayNumber = 3; // Wednesday DayOfWeek wednesday = DayOfWeek.of(dayNumber); System.out.println("Wednesday is represented by the " + wednesday + " constant."); } }
In this example, we create an integer variable dayNumber
and assign it a value of 3, which represents Wednesday. We then call the of()
method on the DayOfWeek
enum, passing in the dayNumber
variable as an argument. The of()
method returns the DayOfWeek
enum constant for Wednesday, which we assign to the wednesday
variable. Finally, we print a message that confirms which enum constant represents Wednesday.
When we run this program, the output will be:
Wednesday is represented by the WEDNESDAY constant.
This shows that the of()
method can be used to retrieve the DayOfWeek
enum constant for a specific day-of-week.
Java DayOfWeek Example: plus()
The plus()
method of the DayOfWeek
enum in Java returns the day-of-week that is a specified number of days after the current day-of-week. The argument passed to the plus()
method should be a long value representing the number of days to add. Here’s an example that demonstrates how to use the plus()
method:
import java.time.DayOfWeek; public class DayOfWeekExample { public static void main(String[] args) { DayOfWeek today = DayOfWeek.WEDNESDAY; long daysToAdd = 4; DayOfWeek futureDay = today.plus(daysToAdd); System.out.println("Today is " + today + ", " + daysToAdd + " days later is " + futureDay + "."); } }
In this example, we create a DayOfWeek
object representing Wednesday and assign it to the today
variable. We then create a long
variable daysToAdd
and assign it a value of 4, indicating that we want to add 4 days to Wednesday. We then call the plus()
method on the today
object, passing in the daysToAdd
variable as an argument. The plus()
method returns a DayOfWeek
object representing the day-of-week that is 4 days after Wednesday, which we assign to the futureDay
variable. Finally, we print a message that confirms the original day-of-week, the number of days added, and the resulting day-of-week.
When we run this program, the output will be:
Today is WEDNESDAY, 4 days later is SUNDAY.
This shows that the plus()
method can be used to calculate the day-of-week that is a certain number of days after the current day-of-week.
Java DayOfWeek Example: minus()
The minus()
method of the DayOfWeek
enum in Java returns the day-of-week that is a specified number of days before the current day-of-week. The argument passed to the minus()
method should be a long value representing the number of days to subtract. Here’s an example that demonstrates how to use the minus()
method:
import java.time.DayOfWeek; public class DayOfWeekExample { public static void main(String[] args) { DayOfWeek today = DayOfWeek.SATURDAY; long daysToSubtract = 2; DayOfWeek pastDay = today.minus(daysToSubtract); System.out.println("Today is " + today + ", " + daysToSubtract + " days ago was " + pastDay + "."); } }
In this example, we create a DayOfWeek
object representing Saturday and assign it to the today
variable. We then create a long
variable daysToSubtract
and assign it a value of 2, indicating that we want to subtract 2 days from Saturday. We then call the minus()
method on the today
object, passing in the daysToSubtract
variable as an argument. The minus()
method returns a DayOfWeek
object representing the day-of-week that is 2 days before Saturday, which we assign to the pastDay
variable. Finally, we print a message that confirms the original day-of-week, the number of days subtracted, and the resulting day-of-week.
When we run this program, the output will be:
Today is SATURDAY, 2 days ago was THURSDAY.
This shows that the minus()
method can be used to calculate the day-of-week that is a certain number of days before the current day-of-week.
Java DayOfWeek Example: getValue()
The getValue()
method of the DayOfWeek
enum in Java returns the integer value corresponding to the day-of-week. The integer value returned by the getValue()
method ranges from 1 to 7, where 1 represents Monday and 7 represents Sunday. Here’s an example that demonstrates how to use the getValue()
method:
import java.time.DayOfWeek; public class DayOfWeekExample { public static void main(String[] args) { DayOfWeek sunday = DayOfWeek.SUNDAY; int sundayValue = sunday.getValue(); System.out.println("Sunday is represented by the value " + sundayValue + "."); } }
In this example, we create a DayOfWeek
object representing Sunday and assign it to the sunday
variable. We then call the getValue()
method on the sunday
object, which returns an integer value representing the day-of-week. We assign this value to the sundayValue
variable. Finally, we print a message that confirms the integer value corresponding to Sunday.
When we run this program, the output will be:
Sunday is represented by the value 7.
This shows that the getValue()
method can be used to retrieve the integer value corresponding to a DayOfWeek
enum constant.