Java-util-calendar-getdisplaynames

提供:Dev Guides
移動先:案内検索

Java.util.Calendar.getDisplayNames()メソッド

説明

  • java.util.Calendar.getDisplayNames()メソッドは、指定された *style および locale のカレンダーフィールドのすべての名前と、対応する field 値を含むマップを返します。

宣言

以下は* java.util.Calendar.getDisplayNames()*メソッドの宣言です

public Map<String,Integer> getDisplayNames(int field,int style,Locale locale)

パラメーター

  • field -カレンダーフィールド。
  • style -文字列表現に適用されるスタイル
  • locale -文字列表現のロケール

戻り値

このメソッドは、スタイルとロケールのすべての表示名とそのフィールド値を含むマップを返します。文字列表現が利用できない場合は null を返します。

例外

  • IllegalArgumentException -フィールドまたはスタイルが無効な場合、またはこのカレンダーが寛容ではなく、いずれかのフィールドに無効な値がある場合
  • NullPointerException -ロケールがnullの場合

次の例は、java.util.calendar.getDisplayNames()メソッドの使用法を示しています。

package com.finddevguides;

import java.util.*;

public class CalendarDemo {
   public static void main(String[] args) {

     //create calendar and locale
      Calendar now = Calendar.getInstance();
      Locale locale = Locale.getDefault();

     //call the getdisplaynames method
      Map< String, Integer> representations =
      now.getDisplayNames(Calendar.DAY_OF_WEEK, Calendar.LONG, locale);
      NavigableMap< String, Integer> navMap =
      new TreeMap< String, Integer>(representations);

     //print the results
      System.out.printf("Whole list:%n%s%n", navMap);
   }
}

上記のプログラムをコンパイルして実行すると、次の結果が生成されます-

Whole list:
{Monday=2, Sunday=1, Thursday=5, Friday=6, Saturday=7, Wednesday=4, Tuesday=3}