Solved

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

Posted on 2013-12-24
3
1,153 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This is an explanation of a simple data model to help parse a JSON feed
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

733 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