Java SE 6 の和暦

Java6では「昭和」とか「平成」のような和暦に対応している。藤原はこれまでこういった処理をしたことがないが、あると便利なのかもしれない。

public static void main(String[] args){
Locale locale = new Locale("ja", "JP", "JP");
Date date = new Date();
//FULL指定でフォーマット
DateFormat japaneseFormat
= DateFormat.getDateInstance(DateFormat.FULL, locale);
System.out.println(japaneseFormat.format(date));
//平成(H)を指定する場合はGxxxxになる
DateFormat format
= new SimpleDateFormat("Gyyyy年 MM月 dd日", locale);
System.out.println(format.format(date));
//平成という文字を使う場合
format
= new SimpleDateFormat("GGGGyyyy年 MM月 dd日", locale);
System.out.println(format.format(date));
}
//実行結果
平成20年6月23日
H20年 06月 23日
平成20年 06月 23日