Java Day End fillBetweenDays(Date from, Date to)

Here you can find the source of fillBetweenDays(Date from, Date to)

Description

fill Between Days

License

Open Source License

Declaration

public static Set<Date> fillBetweenDays(Date from, Date to) 

Method Source Code

//package com.java2s;

import java.util.Calendar;

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

import java.util.Set;
import java.util.TreeSet;

public class Main {
    public static Set<Date> fillBetweenDays(Date from, Date to) {
        Calendar c1 = dateToCalendar(from);
        Calendar c2 = dateToCalendar(to);

        int days = gapInDays(c1, c2);
        Set<Date> fill = new TreeSet<>();
        if (days > 0) {
            for (int i = 1; i < days; i++) {
                c1.add(Calendar.DAY_OF_MONTH, 1);
                fill.add(c1.getTime());// www  . ja  v a  2s .  co m
            }
        }
        return fill;

    }

    public static Calendar dateToCalendar(Date d) {
        Calendar c1 = new GregorianCalendar();
        c1.setTime(d);
        return c1;
    }

    static int gapInDays(Calendar a, Calendar b) {
        //same year
        if (a.get(Calendar.YEAR) == b.get(Calendar.YEAR)) {
            return b.get(Calendar.DAY_OF_YEAR) - a.get(Calendar.DAY_OF_YEAR);
        }
        //consecutive year
        else if (a.get(Calendar.YEAR) + 1 == b.get(Calendar.YEAR)) {
            return a.getMaximum(Calendar.DAY_OF_YEAR) + b.get(Calendar.DAY_OF_YEAR) - a.get(Calendar.DAY_OF_YEAR);

        } else
            return 0;
    }
}

Related

  1. endOfDay(final Date date)
  2. endOfDayDate(Date date)
  3. endOfDayInMillis(long date)
  4. endOfTheMonth(java.util.Date date, Locale locale)
  5. endOfYear(Date referenceDate, TimeZone timeZone)
  6. getAgeInDays(Date startDate, Date endDate)
  7. getApartDate(Date startDate, Date endDate)
  8. getBetweenDays(String strFromDate, String strToDate)
  9. getCalender(Date date)