10
2

More than 3 years have passed since last update.

【Java】LocalDate, DateTimeformatterで取得した曜日を日本語で表示する方法

Last updated at Posted at 2020-04-20

開発環境

・ホストOS: Windows10 Home insider build preview
・ゲストOS: WSL2 Ubuntu18.04 LTS
・VScode ver1.44.2
・Java openjdk11

問題


import java.time.LocalDate;
import java.time.format.DateTimeFormatter;


public class Time {
    public static void main(String[] args) {
        LocalDate localDate = LocalDate.of(2020, 6, 3).plusMonths(1).plusDays(24);

        DateTimeFormatter formmater = DateTimeFormatter.ofPattern("yyyy年MM月dd日(E)");

        String format = localDate.format(formmater);

        System.out.println(format);
    }
}

上記のコードをコンパイルして実行した結果

2020年07月27日(Mon)

と曜日の部分が英語で出力される。(Mon)ではなく(月)と表示したい。

解決法

①java.util.Localeをインポートする
②ofPatternメソッドの第二引数にLocale.JAPANESEを渡して日本語表記を指定する

実際のコードで確認する


import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
//変更部分
import java.util.Locale;


public class Time {
    public static void main(String[] args) {
        LocalDate localDate = LocalDate.of(2020, 6, 3).plusMonths(1).plusDays(24);
        //変更部分                                                         
        DateTimeFormatter formmater = DateTimeFormatter.ofPattern("yyyy年MM月dd日(E)", Locale.JAPANESE);

        String format = localDate.format(formmater);

        System.out.println(format);
    }
}

出力結果は…

2020年07月27日(月)

無事曜日を日本語表記に変更することができた。

参考資料

Locale (Java Platform SE 7)
DateTimeFormatter (Java Platform SE 8)
LocalDate (Java Platform SE 8)

10
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
10
2