Expand my Community achievements bar.

Nomination window for the Adobe Community Advisor Program, Class of 2025, is now open!
SOLVED

Java Calendar class issue

Avatar

Level 4

I have a date property in my dialog with xtype "datetime". I am doing below operations in my taglib while accessing this property:

ValueMap map = currentPage.getProperties();

Calendar cal = (GregorianCalendar) map.get("releaseDate"); // Here let's say month was June.

Date date = cal.getTime();

// some logic in between

cal.add(Calendar.Month, -1); // Month got updated to May.

// some logic in between

cal.add(Calendar.Month, 2); // line number xx // Month got updated to July.

// some logic in between

log.debug(((GregorianCalendar) map.get("releaseDate")).getTime()); // Here it doesn't give me original value back but it gives the modified value from line number xx i.e. July.

 

is there anything I am missing? Seems like it is updating references all the way back.

1 Accepted Solution

Avatar

Correct answer by
Level 2

When you do map.get("releaseDate") you re getting teh Calendar and then getting the Date that is internal to Calendar when using cal.getTime().

You are getting the reference to the object, not a new object. so any change you do to cal object , aka cal.add(), you are changin the original Calendar. 

You need to create a copy of the original Calendar as to keep the original intact. 

This would crate a copy ;

 

copy = Calendar.getInstance(original.getTimeZone());copy.setTime(original.getTime());

View solution in original post

1 Reply

Avatar

Correct answer by
Level 2

When you do map.get("releaseDate") you re getting teh Calendar and then getting the Date that is internal to Calendar when using cal.getTime().

You are getting the reference to the object, not a new object. so any change you do to cal object , aka cal.add(), you are changin the original Calendar. 

You need to create a copy of the original Calendar as to keep the original intact. 

This would crate a copy ;

 

copy = Calendar.getInstance(original.getTimeZone());copy.setTime(original.getTime());