Apr 8, 2015
Apr 8, 2015
N/A Views
MD
warning
この記事は2年以上前に更新されたものです。情報が古くなっている可能性があります。

メモ

JSR-310の和暦、そのままprintすると「Japanese Heisei 27-04-08」な感じになっちゃう。
漢字で表示したいときはDateTimeFormatterを使う。

package perfectjava.entity;

import java.time.chrono.JapaneseChronology;
import java.time.chrono.JapaneseDate;
import java.time.chrono.JapaneseEra;
import java.time.format.DateTimeFormatter;
import java.time.format.ResolverStyle;
import java.util.Locale;

public class Foo {

    public static void main(String[] args) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("Gy年MM月dd日")
            .withLocale(Locale.JAPANESE)
            .withResolverStyle(ResolverStyle.STRICT)
            .withChronology(JapaneseChronology.INSTANCE);
        JapaneseDate date = JapaneseDate.now();
        System.out.println(date); // Japanese Heisei 27-04-08
        System.out.println(date.format(formatter)); // 平成27年04月08日
        JapaneseDate jjugCccDate = JapaneseDate.of(JapaneseEra.HEISEI, 27, 4, 11);
        System.out.println(jjugCccDate.format(formatter)); // 平成27年04月11日
    }

}
Found a mistake? Update the entry.
Share this article: