IK.AM

@making's tech note


JSR-310の和暦漢字表示

🗃 {Programming/Java/java/time}
🏷 Java SE 8 
🗓 Updated at 2015-04-08T06:37:43Z  🗓 Created at 2015-04-08T06:37:43Z   🌎 English Page

メモ

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.FormatStyle;

public class Foo {
    public static void main(String[] args) {
        DateTimeFormatter formatter = DateTimeFormatter
                .ofLocalizedDate(FormatStyle.FULL)
                .withChronology(JapaneseChronology.INSTANCE);
        JapaneseDate date = JapaneseDate.now();
        System.out.println(date); // Japanese Heisei 27-04-08
        System.out.println(date.format(formatter)); // 平成27年4月8日
        JapaneseDate jjugCccDate = JapaneseDate.of(JapaneseEra.HEISEI, 27, 4, 11);
        System.out.println(jjugCccDate.format(formatter)); // 平成27年4月11日
    }
}

✒️️ Edit  ⏰ History  🗑 Delete