Java Calendar class issue | Community
Skip to main content
prohira
New Participant
October 16, 2015
Solved

Java Calendar class issue

  • October 16, 2015
  • 1 reply
  • 762 views

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.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by saulovenancio

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());

1 reply

saulovenancioAccepted solution
New Participant
October 16, 2015

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());