Java Day Between dayDiff(Date d1, Date d2)

Here you can find the source of dayDiff(Date d1, Date d2)

Description

Determines the number of days between two dates

License

Open Source License

Parameter

Parameter Description
d1 a parameter
d2 a parameter

Declaration

public static int dayDiff(Date d1, Date d2) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Main {
    /**//from   w  w  w .j  av  a2 s .  c o  m
     * Determines the number of days between two dates
     *
     * @param d1
     * @param d2
     * @return
     */
    public static int dayDiff(Date d1, Date d2) {
        long timeDiff = removeTime(d1).getTime() - removeTime(d2).getTime();

        // milliseconds in 1 day = 86400000
        return (int) (timeDiff / 86400000);
    }

    /**
     * Sets the time on the given {@link Date} object to midnight
     *
     * @param d
     * @return
     */
    public static Date removeTime(Date d) {
        Calendar inst = GregorianCalendar.getInstance();
        inst.setTime(d);
        inst.set(Calendar.HOUR_OF_DAY, 0);
        inst.set(Calendar.MINUTE, 0);
        inst.set(Calendar.SECOND, 0);
        inst.set(Calendar.MILLISECOND, 0);
        return inst.getTime();
    }
}

Related

  1. calculateDays(Date dateEarly, Date dateLater)
  2. calculateDifference(Date d1, Date d2)
  3. calculateDifferMonths(Date date1, Date date2, boolean isTruncate)
  4. calculateNumberOfDays(String dateStr)
  5. countDiffDay(String beginDateBase, String endDateBase)
  6. dayDiff(Date start, Date end)
  7. daysDiff(final Date data1, final Date data2)
  8. differentDaysByMillisecond(Date date1, Date date2)
  9. diffInDays(Date date1, Date date2)