Saturday, October 18, 2025
HomeLanguagesJavaDayOfWeek valueOf() method in Java with Examples

DayOfWeek valueOf() method in Java with Examples

The valueOf() method of java.time.DayOfWeek is an in-built function in Java which takes a string and returns an instance of DayOfWeek corresponding to the string. The string must match exactly to the identifier used to declare a day-of-week constant in this type. Extraneous white space characters are not permitted.

Method Declaration:

 public static DayOfWeek valueOf(String name)

Syntax:

 DayOfWeek dayOfWeekObject = DayOfWeek.valueOf(String name)

Parameters: This method takes name as parameter where:

  • name – is the string which is the name of the day-of-week.
  • dayOfWeekObject – is an instance of DayOfWeek.
  • Return Value: The function returns an instance of DayOfWeek corresponding to the name string

    Below programs illustrate the above method:
    Program 1:




    // Java Program Demonstrate valueOf()
    // method of DayOfWeek
      
    import java.time.*;
      
    class DayOfWeekExample {
        public static void main(String[] args)
        {
            // Initializing a DayOfWeek instance
            DayOfWeek dayOfWeek
                = DayOfWeek.valueOf("MONDAY");
      
            // Printing the day-of-week
            System.out.println(dayOfWeek.name());
        }
    }

    
    
    Output:

    MONDAY
    

    Program 2:




    // Java Program Demonstrate valueOf()
    // method of DayOfWeek
      
    import java.time.*;
      
    class DayOfWeekExample {
        public static void main(String[] args)
        {
            // Initializing a DayOfWeek instance
            DayOfWeek dayOfWeek
                = DayOfWeek.valueOf("SATURDAY");
      
            // Printing the day-of-week
            System.out.println(dayOfWeek.name());
        }
    }

    
    
    Output:

    SATURDAY
    

    Reference: https://docs.oracle.com/javase/8/docs/api/java/time/DayOfWeek.html#valueOf-java.lang.String-

    RELATED ARTICLES

    Most Popular

    Dominic
    32361 POSTS0 COMMENTS
    Milvus
    88 POSTS0 COMMENTS
    Nango Kala
    6728 POSTS0 COMMENTS
    Nicole Veronica
    11892 POSTS0 COMMENTS
    Nokonwaba Nkukhwana
    11954 POSTS0 COMMENTS
    Shaida Kate Naidoo
    6852 POSTS0 COMMENTS
    Ted Musemwa
    7113 POSTS0 COMMENTS
    Thapelo Manthata
    6805 POSTS0 COMMENTS
    Umr Jansen
    6801 POSTS0 COMMENTS