Home » Java programming language

Java Calendar getDisplayNames() Method with Example

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

Calendar Class getDisplayNames() method

  • getDisplayNames() method is available in java.util package.
  • getDisplayNames() method is used to return Map that contains all field names of the calendar will be updated in the given field(fi) values in the given style(st) and Locale(lo).
  • getDisplayNames() 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.
  • getDisplayNames() method may throw an exception at the time of returning Map object.
    • IllegalArgumentException: This exception may throw when the given field(fi) or style(st) are not valid.
    • NullPointerException: This exception may throw when the given parameter Locale(lo) is null exists.

Syntax:

    public Map getDisplayNames(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 Map object that contains names display in the given style and locale and their desired field(fi) values otherwise it returns null when no string denotation exists.

Example:

// Java Program to demonstrate the example of
// Map getDisplayNames() method of Calendar

import java.util.*;

public class GetDisplayNames {
    public static void main(String args[]) {
        // Instantiating a Calendar object
        Calendar ca = Calendar.getInstance();

        // Instantiating a Locale object
        Locale lo = Locale.getDefault();

        // By using getDisplayNames() method is to
        // display the names
        Map < String, Integer > m = ca.getDisplayNames(Calendar.DAY_OF_WEEK,
            Calendar.LONG, lo);

        NavigableMap < String, Integer > nm = new TreeMap < String, Integer > (m);

        // Displaying the results 
        System.out.println(" ca.getDisplayNames(): " + nm);
    }
}

Output

ca.getDisplayNames(): {Friday=6, Monday=2, Saturday=7, Sunday=1, Thursday=5, Tuesday=3, Wednesday=4}


Comments and Discussions!

Load comments ↻





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