計算兩個日期之間的天數

 

      /**

      * 計算兩個日期之間的天數

      * @param startDate

      * @param endDate

      * @return

      */

      public static long daysBetweenTwoDate(Date startDate , Date endDate ){

 

            long daysBetweenTwoDate = 0;

 

           SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd" );

 

            try {

                 startDate = simpleDateFormat .parse(simpleDateFormat .format(startDate ));

                 endDate = simpleDateFormat .parse(simpleDateFormat .format(endDate ));

           } catch (ParseException e) {

                 logger.error( "日期轉換失敗:" + e .getMessage(), e);

           }

 

            int nd = 1000 * 24 * 60 * 60;

            daysBetweenTwoDate = ( endDate .getTime() - startDate.getTime()) / nd;

 

            return daysBetweenTwoDate;

 

     }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章