I am still using the deprecated setYear() on Java for my calendar program and somehow it displays the same year like this:
Wed Dec 21 00:00:00 CST 2022
Wed Dec 21 00:00:00 CST 2022
Wed Dec 21 00:00:00 CST 2022
//loop in another method
for (int i = realYear-1900; i <= realYear-1900+10; i++){
date.setYear(i);
vectorEvents.add(new Event(date, title));
}
textArea.setText(displayEvents());
//events method
public String displayEvents(){
String data ="";
Event e;
for(int i=0; i<vectorEvents.size(); i++){
e = vectorEvents.get(i);
data += e.date + "\n";
}
return data;
}
I tried displaying after adding the first date and then displaying again after adding the second. It seems to change the first date to the latest year the same as the second one. Is there another solution for this or there is just something wrong. Thank you.
You’re adding the same
Dateobject 10 times to a vector (which shouldn’t be used anymore either, BTW). So at the end of the loop, every reference in the vector points to the same Date object, containing the last year you set on it.You need to instantiate a new Date at each iteration: