java獲得日期之間天數的辦法。本站提示廣大學習愛好者:(java獲得日期之間天數的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是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法式設計有所贊助。