ある日から別の日までの経過日数の取得方法を紹介します。
まず、2つの日付をエポック(1970年1月1日 00:00:00.000 GMT)からの経過ミリ秒に変換し、その差分を求めます。
次に、算出した差分値を日単位に変換してやることで経過日数を取得することができます。
エポックからの経過ミリ秒は Date#getTime()、Calendar#getTimeInMillis() などで取得できます。
サンプルコード
2 | static final int MILLIS_OF_DAY = 1000 * 60 * 60 * 24 ; |
11 | Calendar calendarNow = Calendar.getInstance(); |
12 | Calendar calendarPast = Calendar.getInstance(); |
13 | calendarPast.set( 2000 , 1 , 1 , 0 , 0 , 0 ); |
17 | int diffDays = getDiffDays(calendarNow, calendarPast); |
24 | long getDiffDays(Calendar calendar1, Calendar calendar2) { |
26 | long diffTime = calendar1.getTimeInMillis() - calendar2.getTimeInMillis(); |
30 | long diffDays = diffTime / MILLIS_OF_DAY; |
関連があると思われる記事:
[`google_buzz` not found]
[`yahoo` not found]
[`livedoor` not found]
[`friendfeed` not found]
[`grow` not found]