Javaexamples-date-time-short-month

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

Javaの例-月を短い形式で表示する

問題の説明

月の名前を短い形式で表示するには?

溶液

この例では、DateFormatSymbolsクラスのDateFormatSymbols()。getShortMonths()メソッドを使用して、短い形式で月の名前を表示します。

import java.text.SimpleDateFormat;
import java.text.DateFormatSymbols;

public class Main {
   public static void main(String[] args) {
      String[] shortMonths = new DateFormatSymbols().getShortMonths();

      for (int i = 0; i < (shortMonths.length-1); i++) {
         String shortMonth = shortMonths[i];
         System.out.println("shortMonth = " + shortMonth);
      }
   }
}

結果

上記のコードサンプルは、次の結果を生成します。

shortMonth = Jan
shortMonth = Feb
shortMonth = Mar
shortMonth = Apr
shortMonth = May
shortMonth = Jun
shortMonth = Jul
shortMonth = Aug
shortMonth = Sep
shortMonth = Oct
shortMonth = Nov
shortMonth = Dec

以下は、日付、時刻、および短い月の別のサンプル例です。

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Calendar;

public class Main {
   public static void main(String[] argv) throws Exception {
      String str1 = "dd-MMM-yy";
      Date d = Calendar.getInstance().getTime();
      SimpleDateFormat sdf = new SimpleDateFormat(str1, Locale.FRENCH);
      System.out.println(sdf.format(d));
      sdf = new SimpleDateFormat(str1, Locale.ENGLISH);
      System.out.println(sdf.format(d));
   }
}

結果

上記のコードサンプルは、次の結果を生成します(結果は現在の日付に依存します。

11-nov.-16
11-Nov-16