Java Day From distanceToNowInDaysIgnoreTime(Date date)

Here you can find the source of distanceToNowInDaysIgnoreTime(Date date)

Description

distance To Now In Days Ignore Time

License

Apache License

Declaration

public static float distanceToNowInDaysIgnoreTime(Date date) 

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 distanceToNowInDaysIgnoreTime(Date date) {
        return dateDiffInDaysIgnoreTime(date, new Date());
    }//from www.  jav  a 2  s  .c o  m

    public static float dateDiffInDaysIgnoreTime(Date dateStart, Date dateEnd) {
        return dateDiffInDays(getMidnight(dateStart), getMidnight(dateEnd));
    }

    /**
     * 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. daysInFebruary(int year)
  2. daysInMonthForYear(int commonMonthIndex, int yearInteger)
  3. daysOfTwo(Date date1, Date date2)
  4. daysSince(Date since)
  5. dayStartTime(int day)
  6. equalsDay(final Date date1, final Date date2)
  7. explDay(Date date)
  8. firstDayOfCentury(final int century)
  9. get23HourOfDay(Date date)