Java Day Between getDateDifferenceinDays(Date date1, Date date2)

Here you can find the source of getDateDifferenceinDays(Date date1, Date date2)

Description

get Date Differencein Days

License

Open Source License

Declaration

public static Integer getDateDifferenceinDays(Date date1, Date date2) 

Method Source Code

//package com.java2s;

import java.util.Calendar;

import java.util.Date;

public class Main {
    public static Integer getDateDifferenceinDays(Date date1, Date date2) {

        if (date1 == null || date2 == null)
            return null;

        long diff = 0L;

        diff = Math.abs(date1.getTime() - date2.getTime());
        long oneDayseconds = getSecondsForDay();
        Integer result = (int) (diff / oneDayseconds);
        return result;
    }/*www  .  j a  va  2 s.c om*/

    public static long getSecondsForDay() {

        Date d1 = getDateFor(2009, 10, 10, 00, 00, 00);
        Date d2 = getDateFor(2009, 10, 10, 23, 59, 59);
        long seconds = d2.getTime() - d1.getTime();
        return seconds;
    }

    /**
     * This method returns the date for the passed year month day hours minutes seconds
     * 
     * @param year
     * @param month
     * @param day
     * @param hours
     * @param minutes
     * @param seconds
     * @return
     */
    public static Date getDateFor(int year, int month, int day, int hours,
            int minutes, int seconds) {

        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month);
        cal.set(Calendar.DAY_OF_MONTH, day);
        cal.set(Calendar.HOUR_OF_DAY, hours);
        cal.set(Calendar.MINUTE, minutes);
        cal.set(Calendar.SECOND, seconds);

        return cal.getTime();
    }
}

Related

  1. getBetweenDays(String start, String end)
  2. getBetweenTwoDayCalender(String startDay, String endDay)
  3. getDateDiff(Date date, int day)
  4. getDateDiff(final Date startDate, final Date endDate)
  5. getDateDifference(Date date1, Date date2)
  6. getDateDiffYear(Date date, int diff, String... format)
  7. getDateWithDiff(Date date, int amount)
  8. getDaysBetween(Date date1, Date date2)
  9. getDaysBetween(Date start, Date end)