Thursday, October 9, 2025
HomeLanguagesJavaDayOfWeek from() method in Java with Examples

DayOfWeek from() method in Java with Examples

The from() method of java.time.DayOfWeek is an in-built function in Java which takes a TemporalAccessor defining a date and returns an instance of DayOfWeek corresponding to that date. A TemporalAccessor represents an arbitrary set of date and time information, which this method converts to an instance of DayOfWeek corresponding to that date.

Method Declaration:

 public static DayOfWeek from(TemporalAccessor temporal)

Syntax:

 DayOfWeek dayOfWeekObject = DayOfWeek.from(TemporalAccessor temporal)

Parameters: This method takes temporal as parameter where:

  • temporal – is a TemporalAccessor which represents a date.
  • dayOfWeekObject – is an instance of DayOfWeek.
  • Return Value: The function returns an instance of DayOfWeek corresponding to the date specified by temporal

    Below programs illustrate the above method:
    Program 1:




    // Java Program Demonstrate from()
    // method of DayOfWeek
      
    import java.time.*;
    import java.time.DayOfWeek;
      
    class DayOfWeekExample {
        public static void main(String[] args)
        {
            // Set a local date whose day is found
            LocalDate localDate
                = LocalDate.of(1997, Month.AUGUST, 15);
      
            // Initialize a DayOfWeek object
            // with specified local Date
            DayOfWeek dayOfWeek
                = DayOfWeek.from(localDate);
      
            // Printing the day of the week
            System.out.println("Day of the Week on "
                               + localDate + " - "
                               + dayOfWeek.name());
        }
    }

    
    
    Output:

    Day of the Week on 1997-08-15 - FRIDAY
    

    Program 2:




    // Java Program Demonstrate from()
    // method of DayOfWeek
      
    import java.time.*;
      
    class DayOfWeekExample {
        public static void main(String[] args)
        {
            // Set a local date whose day is found
            LocalDate localDate
                = LocalDate.of(2015, Month.JULY, 13);
      
            // Initialize a DayOfWeek object
            // with specified local Date
            DayOfWeek dayOfWeek
                = DayOfWeek.from(localDate);
      
            // Printing the day of the week
            System.out.println("Day of the Week on "
                               + localDate + " - "
                               + dayOfWeek.name());
        }
    }

    
    
    Output:

    Day of the Week on 2015-07-13 - MONDAY
    

    Reference: https://docs.oracle.com/javase/8/docs/api/java/time/DayOfWeek.html#from-java.time.temporal.TemporalAccessor-

    RELATED ARTICLES

    Most Popular

    Dominic
    32346 POSTS0 COMMENTS
    Milvus
    87 POSTS0 COMMENTS
    Nango Kala
    6715 POSTS0 COMMENTS
    Nicole Veronica
    11877 POSTS0 COMMENTS
    Nokonwaba Nkukhwana
    11940 POSTS0 COMMENTS
    Shaida Kate Naidoo
    6835 POSTS0 COMMENTS
    Ted Musemwa
    7094 POSTS0 COMMENTS
    Thapelo Manthata
    6789 POSTS0 COMMENTS
    Umr Jansen
    6791 POSTS0 COMMENTS