JAva日付間の日数を取得する方法
940 ワード
この例では、javaが日付間の日数を取得する方法について説明します.皆さんの参考にしてください.具体的な実現方法は以下の通りである.
本稿で述べたjavaプログラムの設計に役立つことを願っています.
private int daysBetween(Date now, Date returnDate) {
Calendar cNow = Calendar.getInstance();
Calendar cReturnDate = Calendar.getInstance();
cNow.setTime(now);
cReturnDate.setTime(returnDate);
setTimeToMidnight(cNow);
setTimeToMidnight(cReturnDate);
long todayMs = cNow.getTimeInMillis();
long returnMs = cReturnDate.getTimeInMillis();
long intervalMs = todayMs - returnMs;
return millisecondsToDays(intervalMs);
}
private int millisecondsToDays(long intervalMs) {
return (int) (intervalMs / (1000 * 86400));
}
private void setTimeToMidnight(Calendar calendar) {
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
}
本稿で述べたjavaプログラムの設計に役立つことを願っています.