Solved

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

Posted on 2013-12-24
3
1,032 Views
Last Modified: 2013-12-26
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
Comment
Question by:kpbarem
3 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 350 total points
ID: 39738605
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
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 150 total points
ID: 39738663
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
 

Author Closing Comment

by:kpbarem
ID: 39740191
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now