Java Date Between daysBetweenDate(Integer startDate, Integer endDate)

Here you can find the source of daysBetweenDate(Integer startDate, Integer endDate)

Description

days Between Date

License

Apache License

Declaration

public static Integer daysBetweenDate(Integer startDate, Integer endDate) 

Method Source Code

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

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

public class Main {

    public static Integer daysBetweenDate(Integer startDate, Integer endDate) {
        if (startDate == null || endDate == null) {
            return null;
        }/*from  ww w. j av a2 s  . c o m*/
        Calendar c1 = newCalendar(startDate);
        Calendar c2 = newCalendar(endDate);

        Long lg = (c2.getTimeInMillis() - c1.getTimeInMillis()) / 1000 / 60 / 60 / 24;
        return lg.intValue();
    }

    public static Integer daysBetweenDate(Date startDate, Date endDate) {
        if (startDate == null || endDate == null) {
            return null;
        }
        Long interval = endDate.getTime() - startDate.getTime();
        interval = interval / (24 * 60 * 60 * 1000);
        return interval.intValue();
    }

    public static Calendar newCalendar(int year, int month, int day) {
        Calendar ret = Calendar.getInstance();
        if (year < 100) {
            year = 2000 + year;
        }
        ret.set(year, month - 1, day);
        return ret;
    }

    public static Calendar newCalendar(int date) {
        int year = date / 10000;
        int month = (date % 10000) / 100;
        int day = date % 100;

        Calendar ret = Calendar.getInstance();
        ret.set(year, month - 1, day);
        return ret;
    }
}

Related

  1. daysBetween(String from, String to, String format)
  2. daysBetween(String smdate, String bdate)
  3. daysBetween2Dates(Date d1, Date d2)
  4. daysBetween2Dates(Date startDate, Date endDate)
  5. daysBetweenCalendars(Calendar date1, Calendar date2)
  6. daysBetweenDates(Date beginDate, Date endDate)
  7. daysBetweenMidnight(final Date startDate, final Date endDate)
  8. getBetweenDate(Date startDate, Date endDate)
  9. getBetweenDate(String d1, String d2)