Java Calendar.getDisplayName(int field, int style, Locale locale) example

 Posted On  | Pooja Lakkihal 

In this java tutorial, You will learn how to get the string representation of the calendar field value in the given style and locale.

Returns the string representation of the calendar field value in the given style and locale.

syntax

public String getDisplayName(int field, int style, Locale locale)

Parameters

field - the calendar field for which the string representation is returned.

style - the style applied to the string representation; one of SHORT or LONG.

locale - the locale for the string representation.

Returns

The string representation of the given field in the given style, or null if no string representation is applicable.

Throws

IllegalArgumentException - if field or style is invalid, or if this Calendar is non-lenient and any of the calendar fields have invalid values.

NullPointerException - if locale is null.

Java Calendar getDisplayName(int field, int style, Locale locale) example

  • Create Calendar instance by invoking static method Calendar.getInstance();
  • Locale locale = Locale.getDefault(); It will get the current value of the default locale.
  • calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, locale); It will It will return the calendar field value in the string representation.
package com.technicalkeeda;

import java.util.Calendar;
import java.util.Locale;

public class App {

    public static void main(String[] args) {

        Calendar calendar = Calendar.getInstance();
        Locale locale = Locale.getDefault();

        String string = calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, locale);

        System.out.printf("Month :- " + string);

    }
}

output

Month :- January


© technicalkeeda.com 2018

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0254 seconds.