Open In App

DayOfWeek getDisplayName() method in Java with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

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:




    // 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);
        }
    }

    
    

    Output:

    Monday
    Mon
    M
    

    Program 2:




    // 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);
        }
    }

    
    

    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-



    Last Updated : 20 Mar, 2019
    Like Article
    Save Article
    Previous
    Next
    Share your thoughts in the comments
    Similar Reads