Home » Java programming language

Java Calendar getDisplayName() Method with Example

Calendar Class getDisplayName() method: Here, we are going to learn about the getDisplayName() method of Calendar Class with its syntax and example.
Submitted by Preeti Jain, on January 26, 2020

Calendar Class getDisplayName() method

  • getDisplayName() method is available in java.util package.
  • getDisplayName() method is used to return string denotation of the given calendar field(fi) in the specified style(st) and locale(lo).
  • getDisplayName() method is a non-static method, it is accessible with the class object and if we try to access the method with the class name then we will get an error.
  • getDisplayName() method may throw an exception at the time of returning the calendar field with the given pattern.
    • IllegalArgumentException: This exception may throw when calendar field or style values are not valid.
    • NullPointerException: This exception may throw when the given parameter Locale(lo) is null exists.

Syntax:

    public String getDisplayName(int fi, int st, Locale lo);

Parameter(s):

  • int fi – it represents the field(fi) of this Calendar.
  • int st – it represents the style that will implemented to the string denotation.
  • Locale lo – it represents the locale of the string denotation.

Return value:

The return type of the method is String, it returns string denotation of the given calendar field(fi) in the given style(st) or locale(lo) when exists otherwise it returns null when no string denotation exists.

Example:

// Java Program to demonstrate the example of
// String getDisplayName() method of Calendar

import java.util.*;

public class GetDisplayName {
    public static void main(String args[]) {
        // Creating two objects of Locale
        Locale lo1 = new Locale("Japanese", "Japan");
        Locale lo2 = new Locale("Telgu", "Tamil");

        // Display lo1 and lo2
        System.out.println("Locale 1: " + lo1);
        System.out.println("Locale 2: " + lo2);

        // By using getDisplayName() method
        // is to display the name of locale 1
        String s = lo1.getDisplayName();

        // Displaying the results 
        System.out.println("lo1.getDisplayName(): " + s);

        // By using getDisplayName() method
        // is to display the name of locale
        s = lo2.getDisplayName();

        // Displaying the results 
        System.out.println("lo2.getDisplayName(): " + s);
    }
}

Output

Locale 1: japanese_JAPAN
Locale 2: telgu_TAMIL
lo1.getDisplayName(): japanese (JAPAN)
lo2.getDisplayName(): telgu (TAMIL)


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.