Java Date Difference dateDiffInDaysIgnoreTime(Date dateStart, Date dateEnd)

Here you can find the source of dateDiffInDaysIgnoreTime(Date dateStart, Date dateEnd)

Description

date Diff In Days Ignore Time

License

Apache License

Declaration

public static float dateDiffInDaysIgnoreTime(Date dateStart, Date dateEnd) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.*;

public class Main {
    public static final long ONE_DAY_IN_MILLIS = 60 * 60 * 24 * 1000;

    public static float dateDiffInDaysIgnoreTime(Date dateStart, Date dateEnd) {
        return dateDiffInDays(getMidnight(dateStart), getMidnight(dateEnd));
    }//from  w  ww.  j a v a2  s.  c o  m

    /**
     * computes (dateEnd - dateStart) in days
     * @param dateStart the beginning of the intervalle
     * @param dateEnd the end of the inervalle
     * @return the length of the intervalle in days
     */
    public static float dateDiffInDays(Date dateStart, Date dateEnd) {
        return ((float) (dateEnd.getTime() - dateStart.getTime())) / ONE_DAY_IN_MILLIS;
    }

    /**
     * @param date a date
     * @return a new date with same day, month and year as <code>date</code>,
     *         but with hours, minutes and seconds set to zero.
     */
    public static Date getMidnight(Date date) {
        if (date == null)
            throw new IllegalArgumentException("'date' was null");

        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }
}

Related

  1. dateDiff(String startDate, String endDate)
  2. dateDiff(String startTime, String endTime, String format)
  3. DateDiff(String strDateBegin, String strDateEnd, int iType)
  4. dateDiffer(String time1, String time2, String formatStr)
  5. dateDiffInDays(Date dateStart, Date dateEnd)
  6. dateDiffMonth(Date d1, Date d2)
  7. dateDiffWithNow(Calendar sDate)
  8. dayDiff(Date a, Date b)
  9. dayDiff(Date date1, Date date2)