Solved

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

Posted on 2013-12-24
3
1,066 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

This is about my first experience with programming Arduino.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
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:
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

777 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