LoginSignup
6
6

More than 5 years have passed since last update.

任意の日から何日経過した計算する方法

Posted at
// 今日の0時を算出
Calendar calendarNow = Calendar.getInstance();
calendarNow.set(calendarNow.get(Calendar.YEAR), calendarNow.get(Calendar.MONTH) + 1, calendarNow.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
// 過去の0時を算出
Calendar calendarPast = Calendar.getInstance();
calendarPast.set(2014, 2, 15, 0, 0, 0);
// 経過時間を秒単位で求めて、日に直す
long diffTime = calendarNow.getTimeInMillis() - calendarPast.getTimeInMillis();
long pastDays = diffTime / (1000 * 60 * 60 * 24);

別サイトで、

Calendar calendarNow = Calendar.getInstance();

のみで現在時刻を取れるって書いてあったけど、うまくいかなかったので、

calendarNow.set(calendarNow.get(Calendar.YEAR), calendarNow.get(Calendar.MONTH) + 1, calendarNow.get(Calendar.DAY_OF_MONTH), 0, 0, 0);

を追加した。

6
6
3

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