Here you can find the source of getBetweenDateBuckets(Date from, Date to)
Parameter | Description |
---|---|
from | a parameter |
to | a parameter |
public static List<Date> getBetweenDateBuckets(Date from, Date to)
//package com.java2s; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.List; public class Main { /**/*ww w. j a v a 2 s . c o m*/ * From/to inclusive * @param from * @param to * @return */ public static List<Date> getBetweenDateBuckets(Date from, Date to) { final List<Date> buckets = new ArrayList<>(); if (to.after(from)) { Calendar f = dateToCalendar(from); Calendar t = dateToCalendar(to); int gap = gapInDays(f, t); buckets.add(from); for (int i = 1; i <= gap; i++) { buckets.add(addDays(from, i)); } } return buckets; } 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; } public static Date addDays(Date d, int n) { Calendar c = dateToCalendar(d); c.add(Calendar.DAY_OF_MONTH, n); return c.getTime(); } }