Solved

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

Posted on 2013-12-24
3
965 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 74

Accepted Solution

by:
käµfm³d   👽 earned 350 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
topping1 challenge 7 48
Problem to start Neon 20 49
backtracking recursion  code 19 39
Java JRE greater than 1.6 5 0
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to implement Singleton Design Pattern in Java.

744 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

12 Experts available now in Live!

Get 1:1 Help Now