1
0

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で日付を取得する方法

Posted at

javaで日付を取得する方法は色々あります。

Dateクラス

java.util.Date
Dateクラスはjavaの初期から使われているクラスで日付の取得や表示に使われることが多い。
しかし非推奨のメソッドが多いので注意が必要。

Calendarクラス

java.util.Calendar
CalendarクラスはDateよりも機能が豊富で日付の計算や参照ができるクラス。

ここではcalendarクラスを使用した日付の取得方法を紹介します。

java
import java.util.Calendar;

public class Main {
	public static void main(String args[]){
		//日付を取得
		Calendar cal = Calendar.getInstance();
                //月のみ0から数えるため+1しておくとわかりやすい.
                int month = cal.get(Calendar.MONTH) + 1;
                System.out.println(cal.get(Calendar.YEAR) + month + cal.get(Calendar.DATE));


	}
}

実行結果(2018年6月28日の場合)
2018628

1
0
1

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?