DayOfWeek getDisplayName() method in Java with Examples

The getDisplayName() method of java.time.DayOfWeek is an in-built function in Java which returns the textual representation of the day-of-week according to the specified Locale class parameter and TextStyle. The TextStyle defines three elements ‘FULL’, ‘SHORT’ and ‘NARROW’. Locale class represents a specific language and region of the world.

Method Declaration:

 public String getDisplayName(TextStyle style, Locale locale)

Syntax:

 String text = dayOfWeekObject.getDisplayName(TextStyle style, Locale locale)

Parameters: This method takes two parameters:

  • style – is the TestStyle which can be three elements ‘FULL’, ‘SHORT’ and ‘NARROW’.
  • locale – represents a specific language and region of the world. The default locale is US
  • dayOfWeekObject – is an instance of DayOfWeek.
  • Return Value: The function returns returns the textual representation of the day-of-week according to the specified Locale class parameter and TextStyle.

    Below programs illustrate the above method:
    Program 1:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program Demonstrate getDisplayName()
    // method of DayOfWeek
      
    import java.time.*;
    import java.time.format.TextStyle;
    import java.util.Locale;
      
    class DayOfWeekExample {
        public static void main(String[] args)
        {
            // Initializing a DayOfWeek instance
            DayOfWeek dayOfWeek = DayOfWeek.MONDAY;
      
            // Get textual representation of the
            // day-of-week in FULL style
            String full_name
                = dayOfWeek
                      .getDisplayName(TextStyle.FULL,
                                      Locale.getDefault());
      
            // Get textual representation of the
            // day-of-week in SHORT style
            String short_name
                = dayOfWeek
                      .getDisplayName(TextStyle.SHORT,
                                      Locale.getDefault());
      
            // Get textual representation of the
            // day-of-week in NARROW style
            String narrow_name
                = dayOfWeek
                      .getDisplayName(TextStyle.NARROW,
                                      Locale.getDefault());
      
            // Printing the textual names of the day-of-week
            System.out.println(full_name);
      
            System.out.println(short_name);
      
            System.out.println(narrow_name);
        }
    }

    chevron_right

    
    

    Output:

    Monday
    Mon
    M
    

    Program 2:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program Demonstrate getDisplayName()
    // method of DayOfWeek
      
    import java.time.*;
    import java.time.DayOfWeek;
    import java.time.format.TextStyle;
    import java.util.Locale;
      
    class DayOfWeekExample {
        public static void main(String[] args)
        {
            // Initializing a DayOfWeek instance
            DayOfWeek dayOfWeek = DayOfWeek.WEDNESDAY;
      
            // Get textual representation of the
            // day-of-week in FULL style
            String full_name
                = dayOfWeek
                      .getDisplayName(TextStyle.FULL,
                                      Locale.getDefault());
      
            // Get textual representation of the
            // day-of-week in SHORT style
            String short_name
                = dayOfWeek
                      .getDisplayName(TextStyle.SHORT,
                                      Locale.getDefault());
      
            // Get textual representation of the
            // day-of-week in NARROW style
            String narrow_name
                = dayOfWeek
                      .getDisplayName(TextStyle.NARROW,
                                      Locale.getDefault());
      
            // Printing the textual names of the day-of-week
            System.out.println(full_name);
      
            System.out.println(short_name);
      
            System.out.println(narrow_name);
        }
    }

    chevron_right

    
    

    Output:

    Wednesday
    Wed
    W
    

    Reference: https://docs.oracle.com/javase/8/docs/api/java/time/DayOfWeek.html#getDisplayName-java.time.format.TextStyle-java.util.Locale-



    My Personal Notes arrow_drop_up

    Coder Machine Learner Social Activist Vocalist

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.