3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Javaで新元号試してみた

Last updated at Posted at 2019-04-17

Java SE 8 が新元号に対応したらしい

2019年4月17日公開の Java SE 8 Update 211 にて検証。

Sample.java
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("GGGGy年M月d日")
        .withChronology(JapaneseChronology.INSTANCE)
        .withLocale(Locale.JAPAN);

JapaneseDate jd = JapaneseDate.of(2019, 4, 29);

for (int i = 0; i < 5; i++) {
    System.out.println(dtf.format(jd.plus(i, ChronoUnit.DAYS)));
}
実行結果
平成31年4月29日
平成31年4月30日
令和1年5月1日
令和1年5月2日
令和1年5月3日

令和になりました。やりましたね。

ところで、もうすぐゴールデンウィークですね

元号ばかり気にしていて、新元号の最初の年を「元年」と呼ぶのをすっかり失念していましたよ。トラブルにならないといいですね。

楽しいゴールデンウィークをお過ごしください。

一応「元年」も対応してみた

Sample2.java
DateTimeFormatter dtf = new DateTimeFormatterBuilder()
        .appendText(ChronoField.ERA, TextStyle.FULL)
        .appendText(ChronoField.YEAR_OF_ERA, Collections.singletonMap(1L, "元"))
        .appendLiteral("年")
        .appendValue(ChronoField.MONTH_OF_YEAR)
        .appendLiteral("月")
        .appendValue(ChronoField.DAY_OF_MONTH)
        .appendLiteral("日")
        .toFormatter()
        .withChronology(JapaneseChronology.INSTANCE)
        .withLocale(Locale.JAPAN);

もういっそ西暦にしませんか?(結論)

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?