Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1439
  • Last Modified:

How to create an array of dates between two given Date objects

I have two Date objects in Java (Java.util.Date) that are being passed in to a method I am writing.  I was thinking this would be fairly simple, but I can't think of a straight forward way to do this.  I want to first figure out which date is greater and then figure out the difference between the two in days.  Then I simply want to use a loop to create code that is something along the lines.. of

for (int i = o; i < distance between two dates in days; i++){
Date d = new Date("low date + i days");
dateList.add(d);
}
Date[] dateArray = dateList.ToArray();

I'm not entirely sure how to do this, since i'm not entirely sure how to get the distance between the dates in Days.  I'm also not sure if this is the best method to sue to go about this.  Can anyone help me out?

Thanks
0
kpbarem
Asked:
kpbarem
2 Solutions
 
käµfm³d 👽Commented:
What you can do is first determine which date is the lesser of the two:

e.g.

Date left;
Date right;

// Compare the dates to see which is less
if (firstDate.compareTo(secondDate) < 0) {
    left = firstDate;
    right = secondDate;
}
else {
    right = firstDate;
    left = secondDate;
}

Open in new window


Then, you will want to create a loop that counts up to the greater date by one day each loop iteration:

e.g.

for (int i = 0; left.compareTo(right) < 0; i++) {
    result.add(left);           // Add the date iterator value to the result set
    cal.add(Calendar.DATE, 1);  // Add one day
    left = cal.getTime();       // Update the date iterator to the new date
}

Open in new window


Putting it all together:

private static List<Date> GetRangeOfDates(Date firstDate, Date secondDate) {
	List<Date> result = new ArrayList<Date>();
	Calendar cal = Calendar.getInstance();
	Date left;
	Date right;

	// Compare the dates to see which is less
	if (firstDate.compareTo(secondDate) < 0) {
		left = firstDate;
		right = secondDate;
	}
	else {
		right = firstDate;
		left = secondDate;
	}

	cal.clear();
	cal.setTime(left);  // Seed the calendar with the starting date

	for (int i = 0; left.compareTo(right) < 0; i++) {
		result.add(left);           // Add the date iterator value to the result set
		cal.add(Calendar.DATE, 1);  // Add one day
		left = cal.getTime();       // Update the date iterator to the new date
	}

	return result;
}

Open in new window

0
 
CEHJCommented:
You can try this spin on it too

    public static Date[] getDatesFromTo(Date d1, Date d2) {
        List<Date> dates = new ArrayList<Date>();
        dates.add(d1);
        dates.add(d2);
        Collections.sort(dates);

        Date from = dates.get(0);
        Date to = dates.get(1);
        // Remove temporarily
        dates.clear();

        Calendar cal = Calendar.getInstance();
        cal.setTime(from);

        Date d = from;

        while (!d.after(to)) {
            dates.add(cal.getTime());
            cal.add(Calendar.DATE, 1);
            d = cal.getTime();
        }

        return dates.toArray(new Date[dates.size()]);
    }

Open in new window

0
 
kpbaremAuthor Commented:
I ended up going with the first one but I saw no reason why the second one wouldn't work.

Thank you!!
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now