I currently have an ArrayList filled with dates in the format 2012-06-19 and I am trying to add them all to an array of Dates.
This is the portion of code that is failing me,
listIterator = dateValues.listIterator();
Date [] dates = new Date[dateValues.size()];
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
int i = 0;
try{
while(listIterator.hasNext())
{
//System.out.println(listIterator.next().toString());
dates[i] = dateFormat.parse(listIterator.next().toString());
i++;
}
for(i = 0; i < dates.length;i++)
{
System.out.println(dates[i]);
}
}
catch(Exception e){e.printStackTrace()};
}
The line
//System.out.println(listIterator.next().toString());
will print out every date in the ArrayList. Output looks like,
2007-09-07
2007-09-07
2007-10-05
2007-10-05
2007-10-05
2007-10-05
2007-10-05
but my dateFormat line never adds any values to dates[]. Any help would be appreciated.
And no, it isn’t homework.
Try this:
“Less code is good”, so use the language (foreach loops etc) to keep your code small and clean (like the code above).
Note to pedants before you comment: “less code is good”, as long as it remains readable.