Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Vectors

Status
Not open for further replies.

crmayer

Programmer
Nov 22, 2002
280
US
I am writing a method that takes two dates from user input and then creates a vector of dates with each day between the two.
Example:
User inputs 06/10/2007 and 06/16/2007

Vector of Dates should contain:
06/10/2007
06/11/2007
06/12/2007
06/13/2007
06/14/2007
06/15/2007
06/16/2007

The problem I am running into is I have a for loop that gets the difference between the two days and runs through and adds the begin date plus 1 to the vector.
Then I print the vector, well as you might see, when I print the results from the vector, I get 06/17/2007 for all dates. I am guessing that when I add the date to the vector, it is adding the address of the date, therefore they all technically point to the same date, which by the end of the for loop is 06/17/2007.

How do I put the actual value of the date in the vector instead of the address to that date?

Is that possible?

Any guideance would be appricieated.
Thanks
Chris
 
If I read that correctly, it sounds like you're trying to store and read 2 dates at a time. If that's the case, why not store both dates in each Vector entry? Something like:
Code:
public class Pair <T1, T2>
{
   public T1 First;
   public T2 Second;

   public Pair( T1  first, T2  second )
   {
      First = first;
      Second = second;
   }
}

Vector<Pair> dates = new Vector<Pair>();
dates.add( new Pair<String, String>( "06/10/2007", "06/16/2007" ) );
...
 
Don't add 1 to the original date, rather create a new date from the original, add one to that and then add it to the vector...
Code:
Date newDate = new Date(oldDate.getTime());
//add one day to newDate (however you're doing it)
vector.add(newDate);

Tim
 
Thanks for the responses.
cpjust,
I am not trying to store the user input dates, I am trying to get each day (date) between the two input dates and the begining and end date. That is where I run into the problem with storing them in the vector.

timw,
I have tried that, and it seems to still store the address of the new date in the vector (even though I am not sure how or why, since I declare the "new date" inside the for loop). I would have thought that each time through the loop it would "recreate/allocate" memory for the "new date".

I got this to work, but I might have taken the long route.
I created a new variable of type "String" inside the for loop.
Get the date and convert it to a string and store it in the string variable. Then when adding it to the vector, I am doing something like this:
vector.add(string.ValueOf(string));

Now when I loop through the vector and print the values I get:
06/10/2007
06/11/2007
06/12/2007
06/13/2007
06/14/2007
06/15/2007
06/16/2007

Instead of:
06/17/2007
06/17/2007
06/17/2007
06/17/2007
06/17/2007
06/17/2007
06/17/2007

Again, I am not sure exactly what was happening, but my guess since I was adding the date to the vector and then incrementing the date, was when the loop was completed the date gets incremented one more time, which now means that the date's value is 0617/2007, and that is what was printing for each vectors element.

If there is a better way of doing this, please let me know.

Thanks
Chris
 
Oops. Yes Dian, is didn't spot the Cloneable in the API docs. That is better I think.

Chris, post the relevent piece of your code please.

Tim
 
Here is what I have right now, this is working, but might not be the best way to accomplish this:

Date beginDate;
Date endDate;
Vector dateData = new Vector();

public void setDates()
{
try{
beginDate =
eginDate.convertStringToDate("06/10/2007");
tmpDate = tmpDate.convertStringToDate("06/10/2007");
endDate = endDate.convertStringToDate("06/16/2007");
}catch(Throwable ex){
ex.printStackTrace();
}
}
public void getDates()
{
int tmpDays =
(int) (endDate.dateDiff(Date.DATE, beginDate) + 1);
for(int i=0; i<tmpDays; i++)
{
String date = null;
date = tmpDate.toString();
dateData.add(date.valueOf(date));
tmpDate.add(tmpDate.DAY_OF_YEAR, 1);
}
}

OLD CODE
public void getDates()
{
int tmpDays =
(int) (endDate.dateDiff(Date.DATE, beginDate) + 1);
for(int i=0; i<tmpDays; i++)
{
Date tmpDate = beginDate;
dateData.add(tmpDate);
tmpDate.add(tmpDate.DAY_OF_YEAR, 1);
}
}

Let me know if more code is needed.
 
I imagined that: you're declaring a new Object for each iteration, and you add always the same object, that you add one, it loses scope and loop again.

Try this:

Code:
public void getDates()
{
  int tmpDays = 
      (int) (endDate.dateDiff(Date.DATE, beginDate) + 1);
  Date tmpDate = beginDate;
  for(int i=0; i<tmpDays; i++)
  {
    dateData.add(tmpDate);
    tmpDate.add(tmpDate.DAY_OF_YEAR, 1);
  }
}

If it doesn't work, use the clone method.

Cheers,
Dian
 
Code:
public void getDates()
{
  int tmpDays =
      (int) (endDate.dateDiff(Date.DATE, beginDate) + 1);
  Date tmpDate = beginDate;
  for(int i=0; i<tmpDays; i++)
  {
    Date newDate = (Date)tmpDate.clone();
    dateData.add(newDate );
    newDate.add(newDate .DAY_OF_YEAR, i+1);
  }
}

Tim
 
I don't know this Date-Class, which has Methods like
d.dateDiff (...) or .convertStringToDate("06/10/2007");

tmpDate is not defined - what's that?

Code:
tmpDate.add(tmpDate.DAY_OF_YEAR, 1);
What's that add-Method doing?
Sometimes such Methods modify your Object, but sometimes they return a new Object with modified date, and leave the original object in its state.

Code:
 import java.util.*;
import java.text.*;

public class DateDiff
{
	Vector <Date> dates = new Vector <Date> ();
	
	public DateDiff (String from, String to) throws ParseException
	{
		DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
		try
		{
			Date d1 = df.parse (from);
			Date d2 = df.parse (to);
			
			GregorianCalendar cal = new GregorianCalendar ();
			cal.setTime (d1);
			for (Date d = d1; d.before (d2); cal.add (Calendar.DAY_OF_MONTH, 1))
			{
				d = cal.getTime ();
				dates.add (d);
				// System.out.println (d);
			}
		}
		catch (ParseException e)
		{
			System.err.println (e.getMessage ());
		}		
	}
}
And don't catch Throwable, but the specific Exception you're expecting.

don't visit my homepage:
 
Dian,
I will try that, but I think I have already tried with the Date tmpDate before the for loop.
currently I am NOT creating a new object for each iteration, I call this method, which simply takes the first date and adds a day to it until it gets to the end date.

I will also give the clone options a shot

The methods you are refering to are methods that are defined within our application.

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top