Java Day Between getDaysDiff(Date startDate, Date endDate)

Here you can find the source of getDaysDiff(Date startDate, Date endDate)

Description

Gets the difference in days between the given dates.

License

Open Source License

Parameter

Parameter Description
startDate a parameter
endDate a parameter

Declaration

public static int getDaysDiff(Date startDate, Date endDate) 

Method Source Code

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

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

public class Main {
    /**/*from  w w  w . j av  a  2s.co  m*/
     * Gets the difference in days between the given dates.
     * 
     * @param startDate
     * @param endDate
     * @return
     */
    public static int getDaysDiff(Date startDate, Date endDate) {
        Calendar fromCalendar = Calendar.getInstance();
        fromCalendar.setTime(startDate);
        fromCalendar.set(Calendar.HOUR_OF_DAY, 0);
        fromCalendar.set(Calendar.MINUTE, 0);
        fromCalendar.set(Calendar.SECOND, 0);
        fromCalendar.set(Calendar.MILLISECOND, 0);

        Calendar toCalendar = Calendar.getInstance();
        toCalendar.setTime(endDate);
        toCalendar.set(Calendar.HOUR_OF_DAY, 0);
        toCalendar.set(Calendar.MINUTE, 0);
        toCalendar.set(Calendar.SECOND, 0);
        toCalendar.set(Calendar.MILLISECOND, 0);

        return (int) ((toCalendar.getTime().getTime() - fromCalendar.getTime().getTime()) / (1000 * 60 * 60 * 24));
    }
}

Related

  1. getDaysBetween(Date start, Date end)
  2. getDaysBetween(Date start, Date end)
  3. getDaysBetween(Date startDate, Date endDate)
  4. getDaysBetween(Date startDate, Date endDate)
  5. getDaysBetweenDate(Date begin, Date end)
  6. getDaysDifference(final Date begin, final Date end)
  7. getDiffBetweenQuarter(Date latestDate, Date current)
  8. getDiffDays(Date date1, Date date2)
  9. getDiffDays(String startDate, String endDate)