Wednesday, June 17, 2026
HomeLanguagesJavaDayOfWeek of() method in Java with Examples

DayOfWeek of() method in Java with Examples

The of() method of java.time.DayOfWeek is an in-built function in Java which returns an instance of DayOfWeek from an int value. The int value ranges between 1 (Monday) to 7 (Sunday).

Method Declaration:

public static DayOfWeek of(int dayOfWeek)

Syntax:

DayOfWeek dayOfWeekObject = DayOfWeek.of(int dayOfWeek)

Parameters: This method takes dayOfWeek as parameter where:

  • dayOfWeek – is the int value from 1 (Monday) to 7 (Sunday).
  • dayOfWeekObject – is an instance of the DayOfWeek object.

Return Value: The function returns an instance of DayOfWeek object.

Below programs illustrate the above method:
Program 1:




// Java Program Demonstrate of()
// method of DayOfWeek
import java.time.DayOfWeek;
  
class DayOfWeekExample {
    public static void main(String[] args)
    {
        // Getting an instance of DayOfWeek from int value
        DayOfWeek dayOfWeek = DayOfWeek.of(4);
  
        // Printing the day of the week
        // and its Int value
        System.out.println("Day of the Week - "
                           + dayOfWeek.name());
        System.out.println("Int Value of "
                           + dayOfWeek.name() + " - "
                           + dayOfWeek.getValue());
    }
}


Output:

Day of the Week - THURSDAY
Int Value of THURSDAY - 4

Program 2:




// Java Program Demonstrate of()
// method of DayOfWeek
import java.time.DayOfWeek;
  
class DayOfWeekExample {
    public static void main(String[] args)
    {
        // Getting an instance of DayOfWeek from int value
        DayOfWeek dayOfWeek = DayOfWeek.of(7);
  
        // Printing the day of the week
        // and its Int value
        System.out.println("Day of the Week - "
                           + dayOfWeek.name());
        System.out.println("Int Value of "
                           + dayOfWeek.name() + " - "
                           + dayOfWeek.getValue());
    }
}


Output:

Day of the Week - SUNDAY
Int Value of SUNDAY - 7

Reference: https://docs.oracle.com/javase/8/docs/api/java/time/DayOfWeek.html#of-int-

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

3 COMMENTS

Most Popular

Dominic
32516 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6898 POSTS0 COMMENTS
Nicole Veronica
12014 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS