LoginSignup
0
0

More than 1 year has passed since last update.

はじめてのSimpleDateFormatで引っかかった

Posted at

はじめてのjava.text.SimpleDateFormat

とりあえずフォーマットを定義したらその形で出力できるらしいのでやってみた。

SimpleDateFormat_DD
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("MM月DD日");
String today = sdf.format(date);
System.out.println(today);
// 出力結果
6月161日

えー、なんで。。?

調べたら大文字とか小文字でいろいろ定義がされていて、
大文字のDDは年における日数がでるらしい。ふむ。

ddを小文字に変えた。

SimpleDateFormat_dd
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日"); //ddを小文字に変更
String today = sdf.format(date);
System.out.println(today);

思った通りに出た。

// 出力結果
6月10日

日時パターン

文字 コンポーネント
y 2021;21
M 年における月 11
D 年における日 161
d 月における日 25
F 月における曜日 2
E 曜日の名前 Tuesday;Tue
u 曜日の番号 1(1=月曜)

まとめ

便利なので基本的なパターンを覚えて使いこなしたい。

0
0
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
0
0