Java Day Between daysDiff(final Date data1, final Date data2)

Here you can find the source of daysDiff(final Date data1, final Date data2)

Description

Get the difference in days of two given dates.

License

Open Source License

Parameter

Parameter Description
data1 The first date
data2 The second date

Return

The difference in days of two given dates

Declaration

public static int daysDiff(final Date data1, final Date data2) 

Method Source Code


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

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

public class Main {
    /**//from ww  w .  ja  v a2s . c o  m
     * Get the difference in days of two given dates.
     * 
     * @param data1
     *            The first date
     * @param data2
     *            The second date
     * @return The difference in days of two given dates
     */
    public static int daysDiff(final Date data1, final Date data2) {
        long ini = getCalendarInstance(data1).getTimeInMillis();
        long fim = getCalendarInstance(data2).getTimeInMillis();
        long nroHoras = (fim - ini) / 1000 / 3600;
        int nroDias = (int) nroHoras / 24;
        return nroDias;
    }

    /**
     * Get the calendar instance of a given date
     * @param date
     * The date
     * @return The calendar instance
     */
    public static Calendar getCalendarInstance(final Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }
}

Related

  1. calculateDifferMonths(Date date1, Date date2, boolean isTruncate)
  2. calculateNumberOfDays(String dateStr)
  3. countDiffDay(String beginDateBase, String endDateBase)
  4. dayDiff(Date d1, Date d2)
  5. dayDiff(Date start, Date end)
  6. differentDaysByMillisecond(Date date1, Date date2)
  7. diffInDays(Date date1, Date date2)
  8. getBeteenDays(String begin, String end, String format)
  9. getBetweenDays(long longValue1, long longValue2)