Thursday, September 4, 2025
HomeLanguagesJavaDayOfWeek get() method in Java with Examples

DayOfWeek get() method in Java with Examples

The get() method of java.time.DayOfWeek is an in-built function in Java which takes a TemporalField as parameter and gets the value of the specified field from this day-of-week as an int. The TemporalField is a field of date-time, such as month-of-year or hour-of-minute. Date and time is expressed using such fields. If the field is DAY_OF_WEEK then the value of the day-of-week, from 1 to 7, will be returned. All other ChronoField instances will throw an UnsupportedTemporalTypeException. The int value follows the ISO-8601 standard, from 1 (Monday) to 7 (Sunday).

Method Declaration:

public int get(TemporalField field);

Syntax:

int val = DayOfWeekObject.get(TemporalField field);

Parameters: This method takes field as parameter where:

  • field – is a Temporal field like the Chronafield.DAY_OF_WEEK.
  • DayOfWeekObject – represents the DayOfWeek object.
  • Parameters: This method does not accepts any parameters.

    Return Value: The function returns int value of the day of the week, e.g, 1 for Monday, 2 for Tuesday and so on.

    Below programs illustrate the above method:
    Program 1:




    // Java Program Demonstrate get()
    // method of DayOfWeek
      
    import java.time.*;
    import java.time.DayOfWeek;
    import java.time.temporal.ChronoField;
      
    class DayOfWeekExample {
        public static void main(String[] args)
        {
            // Set a local date whose day is found
            LocalDate localDate
                = LocalDate.of(1947,
                               Month.AUGUST, 15);
      
            // Find the day from the local date
            DayOfWeek dayOfWeek
                = DayOfWeek.from(localDate);
      
            // Printing the day of the week
            // and its Int value
            System.out.println("Day of the Week on "
                               + localDate + " - "
                               + dayOfWeek.name());
      
            int val
                = dayOfWeek.get(ChronoField.DAY_OF_WEEK);
      
            System.out.println("Int Value of "
                               + dayOfWeek.name()
                               + " - " + val);
        }
    }

    
    
    Output:

    Day of the Week on 1947-08-15 - FRIDAY
    Int Value of FRIDAY - 5
    

    Program 2:




    // Java Program Demonstrate get()
    // method of DayOfWeek
    import java.time.*;
    import java.time.DayOfWeek;
    import java.time.temporal.ChronoField;
      
    class DayOfWeekExample {
        public static void main(String[] args)
        {
            // Set a local date whose day is found
            LocalDate localDate
                = LocalDate.of(2015,
                               Month.JULY, 13);
      
            // Find the day from the local date
            DayOfWeek dayOfWeek
                = DayOfWeek.from(localDate);
      
            // Printing the day of the week
            // and its Int value
            System.out.println("Day of the Week on "
                               + localDate + " - "
                               + dayOfWeek.name());
      
            int val
                = dayOfWeek.get(ChronoField.DAY_OF_WEEK);
      
            System.out.println("Int Value of "
                               + dayOfWeek.name()
                               + " - " + val);
        }
    }

    
    
    Output:

    Day of the Week on 2015-07-13 - MONDAY
    Int Value of MONDAY - 1
    

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

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

    Most Popular

    Dominic
    32264 POSTS0 COMMENTS
    Milvus
    81 POSTS0 COMMENTS
    Nango Kala
    6629 POSTS0 COMMENTS
    Nicole Veronica
    11799 POSTS0 COMMENTS
    Nokonwaba Nkukhwana
    11859 POSTS0 COMMENTS
    Shaida Kate Naidoo
    6749 POSTS0 COMMENTS
    Ted Musemwa
    7025 POSTS0 COMMENTS
    Thapelo Manthata
    6698 POSTS0 COMMENTS
    Umr Jansen
    6717 POSTS0 COMMENTS